From 691fb5d92c53796be5151f0f2f4d778311f97381 Mon Sep 17 00:00:00 2001 From: emreakay Date: Mon, 18 Mar 2024 15:04:44 +0300 Subject: [PATCH 1/3] - v2 --- src/Contracts/IssueOwnerModelContract.php | 10 +++++-- src/Models/AIssue.php | 21 +++++++++++++++ src/Traits/AIssueOwner.php | 32 +++++++++++++++++++++++ tests/Models/ExampleIssueOwner.php | 27 +++++++++++++++++++ tests/Unit/AIssueTest.php | 17 ++++++++++++ 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tests/Models/ExampleIssueOwner.php diff --git a/src/Contracts/IssueOwnerModelContract.php b/src/Contracts/IssueOwnerModelContract.php index 675cd2a..80d7578 100644 --- a/src/Contracts/IssueOwnerModelContract.php +++ b/src/Contracts/IssueOwnerModelContract.php @@ -2,6 +2,7 @@ namespace AuroraWebSoftware\AIssue\Contracts; +use AuroraWebSoftware\AIssue\Models\AIssue; use AuroraWebSoftware\Connective\Collections\ConnectiveCollection; use AuroraWebSoftware\Connective\Contracts\ConnectiveContract; use Illuminate\Database\Query\Builder; @@ -11,13 +12,18 @@ */ interface IssueOwnerModelContract extends ConnectiveContract { + public function ownIssue(AIssue $issue): void; + + public function disownIssue(AIssue $issue): void; + + /** * ConnectiveCollection */ - public function getOwningIssues(): ConnectiveCollection; + public function getOwningIssues(): ?ConnectiveCollection; /** * ConnectiveCollection */ - public function scopeAllOwningIssues(Builder $query): ConnectiveCollection; + public function scopeAllOwningIssues(Builder $query): ?ConnectiveCollection; } diff --git a/src/Models/AIssue.php b/src/Models/AIssue.php index ea5b1ad..35256ba 100644 --- a/src/Models/AIssue.php +++ b/src/Models/AIssue.php @@ -8,6 +8,7 @@ use AuroraWebSoftware\ACalendar\Models\Event; use AuroraWebSoftware\ACalendar\Traits\HasEvents; use AuroraWebSoftware\AIssue\Contracts\IssueActorModelContract; +use AuroraWebSoftware\AIssue\Contracts\IssueOwnerModelContract; use AuroraWebSoftware\ArFlow\Contacts\StateableModelContract; use AuroraWebSoftware\ArFlow\Traits\HasState; use AuroraWebSoftware\Connective\Collections\ConnectiveCollection; @@ -180,4 +181,24 @@ public function removeAllObservers(): void $observer->delete(); } } + + public function getOwnerModel(): IssueOwnerModelContract|Model|null + { + return $this->connectives('issue_owner_model')?->first(); + } + + /** + * @throws ConnectionTypeNotSupportedException + * @throws ConnectionTypeException + */ + public function setOwnerModel(IssueActorModelContract&Model $issueOwnerModel): void + { + if ($this->connections('issue_owner_model')) { + $this->connections('issue_owner_model') + ->each(fn (Model $connection) => $connection->delete()); + } + + $this->connectTo($issueOwnerModel, 'issue_owner_model'); + } + } diff --git a/src/Traits/AIssueOwner.php b/src/Traits/AIssueOwner.php index e9c452a..2b5e798 100644 --- a/src/Traits/AIssueOwner.php +++ b/src/Traits/AIssueOwner.php @@ -2,11 +2,37 @@ namespace AuroraWebSoftware\AIssue\Traits; +use AuroraWebSoftware\AIssue\Contracts\IssueActorModelContract; use AuroraWebSoftware\AIssue\Contracts\IssueOwnerModelContract; +use AuroraWebSoftware\AIssue\Models\AIssue; use AuroraWebSoftware\Connective\Collections\ConnectiveCollection; +use AuroraWebSoftware\Connective\Exceptions\ConnectionTypeException; +use AuroraWebSoftware\Connective\Exceptions\ConnectionTypeNotSupportedException; +use Illuminate\Database\Query\Builder; trait AIssueOwner { + /** + * @throws ConnectionTypeNotSupportedException + * @throws ConnectionTypeException + */ + public function ownIssue(AIssue $issue): void + { + $issue->connectTo($this, 'issue_owner_model'); + } + + public function disownIssue(AIssue $issue): void + { + // todo + foreach ($this->getOwningIssues() ?? [] as $issueItem) { + if ($issue->getId() === $issueItem->getId()) { + $issue->connections('issue_owner_model')->delete(); + break; + } + } + } + + /** * ConnectiveCollection */ @@ -18,4 +44,10 @@ public function getOwningIssues(): ConnectiveCollection return $this->inverseConnectives('issue_owner_model'); } + + public function scopeAllOwningIssues(Builder $query): ?ConnectiveCollection + { + // todo will be implemented + return ConnectiveCollection::make(); + } } diff --git a/tests/Models/ExampleIssueOwner.php b/tests/Models/ExampleIssueOwner.php new file mode 100644 index 0000000..b5e1fbd --- /dev/null +++ b/tests/Models/ExampleIssueOwner.php @@ -0,0 +1,27 @@ +timestamps(); }); + Schema::create('example_issue_owners', function (Blueprint $table) { + $table->id(); + $table->string('name'); + $table->timestamps(); + }); + + $classArflow = require __DIR__.'/../../vendor/aurorawebsoftware/arflow/database/migrations/create_arflow_history_table.php'; (new $classArflow)->up(); @@ -192,4 +200,13 @@ expect($issue->currentState())->toEqual('state2'); + + $exampleIssueOwner1 = ExampleIssueOwner::create(['name' => 'example issue owner 1']); + $exampleIssueOwner1->ownIssue($issue); + expect($exampleIssueOwner1->getOwningIssues())->toHaveCount(1); + + // delete kısmı yazılmadı henüz + + + }); From de21b6fe81053f6c56ed065027e7238dc3a33fbd Mon Sep 17 00:00:00 2001 From: emreakay Date: Mon, 18 Mar 2024 12:05:11 +0000 Subject: [PATCH 2/3] Fix styling --- src/Contracts/IssueOwnerModelContract.php | 1 - src/Models/AIssue.php | 1 - src/Traits/AIssueOwner.php | 2 -- tests/Models/ExampleIssueOwner.php | 5 ++--- tests/Unit/AIssueTest.php | 4 ---- 5 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/Contracts/IssueOwnerModelContract.php b/src/Contracts/IssueOwnerModelContract.php index 80d7578..34d42ed 100644 --- a/src/Contracts/IssueOwnerModelContract.php +++ b/src/Contracts/IssueOwnerModelContract.php @@ -16,7 +16,6 @@ public function ownIssue(AIssue $issue): void; public function disownIssue(AIssue $issue): void; - /** * ConnectiveCollection */ diff --git a/src/Models/AIssue.php b/src/Models/AIssue.php index 35256ba..9fbedaa 100644 --- a/src/Models/AIssue.php +++ b/src/Models/AIssue.php @@ -200,5 +200,4 @@ public function setOwnerModel(IssueActorModelContract&Model $issueOwnerModel): v $this->connectTo($issueOwnerModel, 'issue_owner_model'); } - } diff --git a/src/Traits/AIssueOwner.php b/src/Traits/AIssueOwner.php index 2b5e798..426d54e 100644 --- a/src/Traits/AIssueOwner.php +++ b/src/Traits/AIssueOwner.php @@ -2,7 +2,6 @@ namespace AuroraWebSoftware\AIssue\Traits; -use AuroraWebSoftware\AIssue\Contracts\IssueActorModelContract; use AuroraWebSoftware\AIssue\Contracts\IssueOwnerModelContract; use AuroraWebSoftware\AIssue\Models\AIssue; use AuroraWebSoftware\Connective\Collections\ConnectiveCollection; @@ -32,7 +31,6 @@ public function disownIssue(AIssue $issue): void } } - /** * ConnectiveCollection */ diff --git a/tests/Models/ExampleIssueOwner.php b/tests/Models/ExampleIssueOwner.php index b5e1fbd..2558509 100644 --- a/tests/Models/ExampleIssueOwner.php +++ b/tests/Models/ExampleIssueOwner.php @@ -9,12 +9,13 @@ /** * @property string $name + * * @method static ExampleIssueOwner create(array $attributes = []) */ class ExampleIssueOwner extends Model implements IssueOwnerModelContract { - use Connective; use AIssueOwner; + use Connective; protected $guarded = []; @@ -22,6 +23,4 @@ public static function supportedConnectionTypes(): array { return []; } - - } diff --git a/tests/Unit/AIssueTest.php b/tests/Unit/AIssueTest.php index ad2a692..bcce237 100644 --- a/tests/Unit/AIssueTest.php +++ b/tests/Unit/AIssueTest.php @@ -26,7 +26,6 @@ $table->timestamps(); }); - $classArflow = require __DIR__.'/../../vendor/aurorawebsoftware/arflow/database/migrations/create_arflow_history_table.php'; (new $classArflow)->up(); @@ -200,13 +199,10 @@ expect($issue->currentState())->toEqual('state2'); - $exampleIssueOwner1 = ExampleIssueOwner::create(['name' => 'example issue owner 1']); $exampleIssueOwner1->ownIssue($issue); expect($exampleIssueOwner1->getOwningIssues())->toHaveCount(1); // delete kısmı yazılmadı henüz - - }); From e1e7cff9c3115e30bf80e5b9605dd1f9e9847fc2 Mon Sep 17 00:00:00 2001 From: emreakay Date: Mon, 18 Mar 2024 15:06:15 +0300 Subject: [PATCH 3/3] - v2 --- tests/Unit/AIssueTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/AIssueTest.php b/tests/Unit/AIssueTest.php index bcce237..6cd4263 100644 --- a/tests/Unit/AIssueTest.php +++ b/tests/Unit/AIssueTest.php @@ -203,6 +203,6 @@ $exampleIssueOwner1->ownIssue($issue); expect($exampleIssueOwner1->getOwningIssues())->toHaveCount(1); - // delete kısmı yazılmadı henüz + // todo delete kısmı yazılmadı henüz });