Skip to content

Commit

Permalink
Create a space title based on the related model (#15)
Browse files Browse the repository at this point in the history
* Create a space title based on the related model

* fix
  • Loading branch information
mako321 authored Aug 29, 2023
1 parent 1f621d3 commit 1aeb5c2
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": ">=5.0",
"darkaonline/l5-swagger": "8.1.0"
"escolalms/courses": "^0"
},
"license": "MIT",
"authors": [
Expand Down
3 changes: 2 additions & 1 deletion src/Jobs/CreatePencilSpaceJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use EscolaLms\ConsultationAccess\Enum\MeetingLinkTypeEnum;
use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryApprovedEvent;
use EscolaLms\ConsultationAccess\Jobs\Strategies\SpaceTitleStrategyFactory;
use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiry;
use EscolaLms\ConsultationAccess\Repositories\Contracts\ConsultationAccessEnquiryRepositoryContract;
use EscolaLms\PencilSpaces\Facades\PencilSpace;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function handle(ConsultationAccessEnquiryRepositoryContract $consultation
}

$resource = new CreatePencilSpaceResource(
$enquiry->title ?? 'Consultation X ' . $enquiry->user->name,
SpaceTitleStrategyFactory::create($enquiry)->getTitle(),
collect($enquiry->consultation->author_id),
collect($enquiry->user_id)
);
Expand Down
26 changes: 26 additions & 0 deletions src/Jobs/Strategies/DefaultSpaceTitleStrategy.php
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'),
);
}
}
11 changes: 11 additions & 0 deletions src/Jobs/Strategies/SpaceTitleRelatedToCourseStrategy.php
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();
}
}
11 changes: 11 additions & 0 deletions src/Jobs/Strategies/SpaceTitleRelatedToLessonStrategy.php
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();
}
}
11 changes: 11 additions & 0 deletions src/Jobs/Strategies/SpaceTitleRelatedToTopicStrategy.php
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();
}
}
8 changes: 8 additions & 0 deletions src/Jobs/Strategies/SpaceTitleStrategy.php
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;
}
26 changes: 26 additions & 0 deletions src/Jobs/Strategies/SpaceTitleStrategyFactory.php
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);
}
}
}
1 change: 1 addition & 0 deletions src/Models/ConsultationAccessEnquiry.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @property-read Consultation $consultation
* @property-read Collection|ConsultationAccessEnquiryProposedTerm[] $consultationAccessEnquiryProposedTerms
* @property-read ?ConsultationUserPivot $consultationUser
* @property-read ?Model $related
*/
class ConsultationAccessEnquiry extends Model
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/CreatePencilSpaceJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace EscolaLms\ConsultationAccess\Tests\Feature;

use EscolaLms\Auth\Models\User;
use EscolaLms\ConsultationAccess\Enum\EnquiryStatusEnum;
use EscolaLms\ConsultationAccess\Enum\MeetingLinkTypeEnum;
use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryApprovedEvent;
use EscolaLms\ConsultationAccess\Jobs\CreatePencilSpaceJob;
use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiry;
use EscolaLms\ConsultationAccess\Tests\TestCase;
use EscolaLms\Consultations\Models\Consultation;
use EscolaLms\Consultations\Models\ConsultationUserPivot;
use EscolaLms\Core\Tests\CreatesUsers;
use EscolaLms\PencilSpaces\Facades\PencilSpace;
use Illuminate\Support\Facades\Event;
Expand All @@ -28,12 +31,20 @@ public function testCreatePencilSpace(): void
PencilSpace::fake();
Event::fake([ConsultationAccessEnquiryApprovedEvent::class]);

$consultationUser = ConsultationUserPivot::factory()
->state([
'user_id' => User::factory(),
'consultation_id' => Consultation::factory(),
])
->create();

/** @var ConsultationAccessEnquiry $enquiry */
$enquiry = ConsultationAccessEnquiry::factory()
->state([
'status' => EnquiryStatusEnum::APPROVED,
'meeting_link' => null,
'meeting_link_type' => null,
'consultation_user_id' => $consultationUser->getKey(),
])
->create();

Expand Down
112 changes: 112 additions & 0 deletions tests/Feature/SpaceTitleStrategyTest.php
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')
);
}
}

0 comments on commit 1aeb5c2

Please sign in to comment.