Skip to content

Commit 81a546b

Browse files
oliverkleeSam Tuke
authored andcommitted
[FEATURE] Interface for domain models (#274)
This interface removes the need to have the generic "object" type for methods that use a domain model. It also will allow subscriptions to get saved using the Repository.save method.
1 parent 71198aa commit 81a546b

File tree

13 files changed

+76
-9
lines changed

13 files changed

+76
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
77
## x.y.z
88

99
### Added
10+
- Interface for domain models (#274)
1011
- Trait for database tests and abstract web test (#264)
1112
- Repository methods for querying subscriptions (#253)
1213
- Bidirectional m:n association Subscribers/SubscriberLists (#254)

src/Domain/Model/Identity/Administrator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
99
use Doctrine\ORM\Mapping\Table;
1010
use PhpList\PhpList4\Domain\Model\Interfaces\CreationDate;
11+
use PhpList\PhpList4\Domain\Model\Interfaces\DomainModel;
1112
use PhpList\PhpList4\Domain\Model\Interfaces\Identity;
1213
use PhpList\PhpList4\Domain\Model\Interfaces\ModificationDate;
1314
use PhpList\PhpList4\Domain\Model\Traits\CreationDateTrait;
@@ -24,7 +25,7 @@
2425
*
2526
* @author Oliver Klee <[email protected]>
2627
*/
27-
class Administrator implements Identity, CreationDate, ModificationDate
28+
class Administrator implements DomainModel, Identity, CreationDate, ModificationDate
2829
{
2930
use IdentityTrait;
3031
use CreationDateTrait;

src/Domain/Model/Identity/AdministratorToken.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use JMS\Serializer\Annotation\ExclusionPolicy;
1414
use JMS\Serializer\Annotation\Expose;
1515
use PhpList\PhpList4\Domain\Model\Interfaces\CreationDate;
16+
use PhpList\PhpList4\Domain\Model\Interfaces\DomainModel;
1617
use PhpList\PhpList4\Domain\Model\Interfaces\Identity;
1718
use PhpList\PhpList4\Domain\Model\Traits\IdentityTrait;
1819

@@ -25,7 +26,7 @@
2526
*
2627
* @author Oliver Klee <[email protected]>
2728
*/
28-
class AdministratorToken implements Identity, CreationDate
29+
class AdministratorToken implements DomainModel, Identity, CreationDate
2930
{
3031
use IdentityTrait;
3132

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PhpList\PhpList4\Domain\Model\Interfaces;
5+
6+
/**
7+
* This is the interface all domain models (entities) should implement.
8+
*
9+
* This allows methods that work with domain models to have a narrow type hinting
10+
* instead of the generic "object".
11+
*
12+
* @author Oliver Klee <[email protected]>
13+
*/
14+
interface DomainModel
15+
{
16+
}

src/Domain/Model/Messaging/SubscriberList.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use JMS\Serializer\Annotation\Expose;
2020
use PhpList\PhpList4\Domain\Model\Identity\Administrator;
2121
use PhpList\PhpList4\Domain\Model\Interfaces\CreationDate;
22+
use PhpList\PhpList4\Domain\Model\Interfaces\DomainModel;
2223
use PhpList\PhpList4\Domain\Model\Interfaces\Identity;
2324
use PhpList\PhpList4\Domain\Model\Interfaces\ModificationDate;
2425
use PhpList\PhpList4\Domain\Model\Traits\CreationDateTrait;
@@ -36,7 +37,7 @@
3637
*
3738
* @author Oliver Klee <[email protected]>
3839
*/
39-
class SubscriberList implements Identity, CreationDate, ModificationDate
40+
class SubscriberList implements DomainModel, Identity, CreationDate, ModificationDate
4041
{
4142
use IdentityTrait;
4243
use CreationDateTrait;

src/Domain/Model/Subscription/Subscriber.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use JMS\Serializer\Annotation\ExclusionPolicy;
1818
use JMS\Serializer\Annotation\Expose;
1919
use PhpList\PhpList4\Domain\Model\Interfaces\CreationDate;
20+
use PhpList\PhpList4\Domain\Model\Interfaces\DomainModel;
2021
use PhpList\PhpList4\Domain\Model\Interfaces\Identity;
2122
use PhpList\PhpList4\Domain\Model\Interfaces\ModificationDate;
2223
use PhpList\PhpList4\Domain\Model\Traits\CreationDateTrait;
@@ -34,7 +35,7 @@
3435
*
3536
* @author Oliver Klee <[email protected]>
3637
*/
37-
class Subscriber implements Identity, CreationDate, ModificationDate
38+
class Subscriber implements DomainModel, Identity, CreationDate, ModificationDate
3839
{
3940
use IdentityTrait;
4041
use CreationDateTrait;

src/Domain/Model/Subscription/Subscription.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use JMS\Serializer\Annotation\ExclusionPolicy;
1515
use JMS\Serializer\Annotation\Expose;
1616
use PhpList\PhpList4\Domain\Model\Interfaces\CreationDate;
17+
use PhpList\PhpList4\Domain\Model\Interfaces\DomainModel;
1718
use PhpList\PhpList4\Domain\Model\Interfaces\ModificationDate;
1819
use PhpList\PhpList4\Domain\Model\Messaging\SubscriberList;
1920
use PhpList\PhpList4\Domain\Model\Traits\CreationDateTrait;
@@ -30,7 +31,7 @@
3031
*
3132
* @author Oliver Klee <[email protected]>
3233
*/
33-
class Subscription implements CreationDate, ModificationDate
34+
class Subscription implements DomainModel, CreationDate, ModificationDate
3435
{
3536
use CreationDateTrait;
3637
use ModificationDateTrait;

src/Domain/Repository/AbstractRepository.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace PhpList\PhpList4\Domain\Repository;
55

66
use Doctrine\ORM\EntityRepository;
7-
use PhpList\PhpList4\Domain\Model\Interfaces\Identity;
7+
use PhpList\PhpList4\Domain\Model\Interfaces\DomainModel;
88

99
/**
1010
* Base class for repositories.
@@ -14,16 +14,16 @@
1414
abstract class AbstractRepository extends EntityRepository
1515
{
1616
/**
17-
* Persists and $model and flushes the entity manager change list.
17+
* Persists $model and flushes the entity manager change list.
1818
*
1919
* This method allows controllers to not depend on the entity manager, but only on the repositories instead,
2020
* following the Law of Demeter.
2121
*
22-
* @param Identity $model
22+
* @param DomainModel $model
2323
*
2424
* @return void
2525
*/
26-
public function save(Identity $model)
26+
public function save(DomainModel $model)
2727
{
2828
$this->getEntityManager()->persist($model);
2929
$this->getEntityManager()->flush();

tests/Unit/Domain/Model/Identity/AdministratorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace PhpList\PhpList4\Tests\Unit\Domain\Model\Identity;
55

66
use PhpList\PhpList4\Domain\Model\Identity\Administrator;
7+
use PhpList\PhpList4\Domain\Model\Interfaces\DomainModel;
78
use PhpList\PhpList4\TestingSupport\Traits\ModelTestTrait;
89
use PhpList\PhpList4\TestingSupport\Traits\SimilarDatesAssertionTrait;
910
use PHPUnit\Framework\TestCase;
@@ -28,6 +29,14 @@ protected function setUp()
2829
$this->subject = new Administrator();
2930
}
3031

32+
/**
33+
* @test
34+
*/
35+
public function isDomainModel()
36+
{
37+
static::assertInstanceOf(DomainModel::class, $this->subject);
38+
}
39+
3140
/**
3241
* @test
3342
*/

tests/Unit/Domain/Model/Identity/AdministratorTokenTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use PhpList\PhpList4\Domain\Model\Identity\Administrator;
77
use PhpList\PhpList4\Domain\Model\Identity\AdministratorToken;
8+
use PhpList\PhpList4\Domain\Model\Interfaces\DomainModel;
89
use PhpList\PhpList4\TestingSupport\Traits\ModelTestTrait;
910
use PhpList\PhpList4\TestingSupport\Traits\SimilarDatesAssertionTrait;
1011
use PHPUnit\Framework\TestCase;
@@ -29,6 +30,14 @@ protected function setUp()
2930
$this->subject = new AdministratorToken();
3031
}
3132

33+
/**
34+
* @test
35+
*/
36+
public function isDomainModel()
37+
{
38+
static::assertInstanceOf(DomainModel::class, $this->subject);
39+
}
40+
3241
/**
3342
* @test
3443
*/

0 commit comments

Comments
 (0)