-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a space title based on the related model (#15)
* Create a space title based on the related model * fix
- Loading branch information
Showing
11 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace EscolaLms\ConsultationAccess\Jobs\Strategies; | ||
|
||
use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiry; | ||
use Illuminate\Support\Carbon; | ||
|
||
class DefaultSpaceTitleStrategy implements SpaceTitleStrategy | ||
{ | ||
protected ConsultationAccessEnquiry $enquiry; | ||
|
||
public function __construct(ConsultationAccessEnquiry $enquiry) | ||
{ | ||
$this->enquiry = $enquiry; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return sprintf( | ||
'%s (%d) %s', | ||
$this->enquiry->user->name, | ||
$this->enquiry->user->getKey(), | ||
Carbon::make($this->enquiry->consultationUser->executed_at)->format('d-m-Y'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace EscolaLms\ConsultationAccess\Jobs\Strategies; | ||
|
||
class SpaceTitleRelatedToCourseStrategy extends DefaultSpaceTitleStrategy | ||
{ | ||
public function getTitle(): string | ||
{ | ||
return $this->enquiry->related->title . ' ' . parent::getTitle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace EscolaLms\ConsultationAccess\Jobs\Strategies; | ||
|
||
class SpaceTitleRelatedToLessonStrategy extends DefaultSpaceTitleStrategy | ||
{ | ||
public function getTitle(): string | ||
{ | ||
return $this->enquiry->related->course->title . ' ' . parent::getTitle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace EscolaLms\ConsultationAccess\Jobs\Strategies; | ||
|
||
class SpaceTitleRelatedToTopicStrategy extends DefaultSpaceTitleStrategy | ||
{ | ||
public function getTitle(): string | ||
{ | ||
return $this->enquiry->related->lesson->course->title . ' ' . parent::getTitle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace EscolaLms\ConsultationAccess\Jobs\Strategies; | ||
|
||
interface SpaceTitleStrategy | ||
{ | ||
public function getTitle(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace EscolaLms\ConsultationAccess\Jobs\Strategies; | ||
|
||
use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiry; | ||
|
||
final class SpaceTitleStrategyFactory | ||
{ | ||
public static function create(ConsultationAccessEnquiry $enquiry): SpaceTitleStrategy | ||
{ | ||
if(!$enquiry->related_type || !class_exists($enquiry->related_type) || !$enquiry->related) { | ||
return new DefaultSpaceTitleStrategy($enquiry); | ||
} | ||
|
||
switch ($enquiry->related_type){ | ||
case \EscolaLms\Courses\Models\Topic::class: | ||
return new SpaceTitleRelatedToTopicStrategy($enquiry); | ||
case \EscolaLms\Courses\Models\Lesson::class: | ||
return new SpaceTitleRelatedToLessonStrategy($enquiry); | ||
case \EscolaLms\Courses\Models\Course::class: | ||
return new SpaceTitleRelatedToCourseStrategy($enquiry); | ||
default: | ||
return new DefaultSpaceTitleStrategy($enquiry); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace EscolaLms\ConsultationAccess\Tests\Feature; | ||
|
||
use Carbon\Carbon; | ||
use EscolaLms\Auth\Models\User; | ||
use EscolaLms\ConsultationAccess\Enum\EnquiryStatusEnum; | ||
use EscolaLms\ConsultationAccess\Jobs\Strategies\SpaceTitleStrategyFactory; | ||
use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiry; | ||
use EscolaLms\ConsultationAccess\Tests\TestCase; | ||
use EscolaLms\Consultations\Models\Consultation; | ||
use EscolaLms\Consultations\Models\ConsultationUserPivot; | ||
use EscolaLms\Courses\Models\Course; | ||
use EscolaLms\Courses\Models\Lesson; | ||
use EscolaLms\Courses\Models\Topic; | ||
|
||
class SpaceTitleStrategyTest extends TestCase | ||
{ | ||
private ConsultationAccessEnquiry $enquiry; | ||
private ConsultationUserPivot $consultationUser; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->consultationUser = ConsultationUserPivot::factory() | ||
->state([ | ||
'user_id' => User::factory(), | ||
'consultation_id' => Consultation::factory(), | ||
]) | ||
->create(); | ||
|
||
/** @var ConsultationAccessEnquiry $enquiry */ | ||
$this->enquiry = ConsultationAccessEnquiry::factory() | ||
->state([ | ||
'status' => EnquiryStatusEnum::APPROVED, | ||
'meeting_link' => null, | ||
'meeting_link_type' => null, | ||
'consultation_user_id' => $this->consultationUser->getKey(), | ||
]) | ||
->create(); | ||
} | ||
|
||
public function testDefaultSpaceTitle(): void | ||
{ | ||
$title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); | ||
$this->assertEquals($this->getDefaultTitle(), $title); | ||
} | ||
|
||
public function testSpaceTitleWhenTopicIsRelated(): void | ||
{ | ||
$course = Course::factory()->create(); | ||
$lesson = Lesson::factory()->state(['course_id' => $course->getKey()])->create(); | ||
$topic = Topic::factory()->state(['lesson_id' => $lesson->getKey()])->create(); | ||
|
||
$this->enquiry->update([ | ||
'related_type' => Topic::class, | ||
'related_id' => $topic->getKey(), | ||
]); | ||
$title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); | ||
$this->assertEquals($course->title . ' ' . $this->getDefaultTitle(), $title); | ||
} | ||
|
||
public function testSpaceTitleWhenLessonIsRelated(): void | ||
{ | ||
$course = Course::factory()->create(); | ||
$lesson = Lesson::factory()->state(['course_id' => $course->getKey()])->create(); | ||
|
||
$this->enquiry->update([ | ||
'related_type' => Lesson::class, | ||
'related_id' => $lesson->getKey(), | ||
]); | ||
$title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); | ||
$this->assertEquals($course->title . ' ' . $this->getDefaultTitle(), $title); | ||
} | ||
|
||
public function testSpaceTitleWhenCourseIsRelated(): void | ||
{ | ||
$course = Course::factory()->create(); | ||
|
||
$this->enquiry->update([ | ||
'related_type' => Course::class, | ||
'related_id' => $course->getKey(), | ||
]); | ||
$title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); | ||
$this->assertEquals($course->title . ' ' . $this->getDefaultTitle(), $title); | ||
} | ||
|
||
public function testSpaceTitleWhenRelatedModelNotExist(): void | ||
{ | ||
$course = Course::factory()->create(); | ||
|
||
$this->enquiry->update([ | ||
'related_type' => Course::class, | ||
'related_id' => $course->getKey(), | ||
]); | ||
|
||
$course->delete(); | ||
$title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); | ||
$this->assertEquals($this->getDefaultTitle(), $title); | ||
} | ||
|
||
private function getDefaultTitle(): string | ||
{ | ||
return sprintf( | ||
'%s (%d) %s', | ||
$this->enquiry->user->name, | ||
$this->enquiry->user->getKey(), | ||
Carbon::make($this->consultationUser->executed_at)->format('d-m-Y') | ||
); | ||
} | ||
} |