Skip to content

Commit f1fa452

Browse files
author
Codeliner
committed
Add domain entity Voyage
1 parent cc96c6d commit f1fa452

File tree

11 files changed

+397
-2
lines changed

11 files changed

+397
-2
lines changed

module/Application/config/module.config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
'types' => array(
133133
'uid' => 'Application\Infrastructure\Persistence\Doctrine\Type\UID',
134134
'trackingid' => 'Application\Infrastructure\Persistence\Doctrine\Type\TrackingId',
135+
'voyagenumber' => 'Application\Infrastructure\Persistence\Doctrine\Type\VoyageNumber',
135136
),
136137
),
137138
),

module/Application/src/Application/Domain/Model/Cargo/Cargo.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace Application\Domain\Model\Cargo;
1010

1111
use Application\Domain\Shared\EntityInterface;
12-
use Application\Domain\Shared\UID;
1312
use Doctrine\ORM\Mapping\Entity;
1413
use Doctrine\ORM\Mapping\Table;
1514
use Doctrine\ORM\Mapping\Column;
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Domain\Model\Voyage;
10+
11+
use Application\Domain\Shared\EntityInterface;
12+
use Doctrine\ORM\Mapping\Entity;
13+
use Doctrine\ORM\Mapping\Table;
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Id;
16+
/**
17+
* A Voyage. Cargos can be booked on a Voyage.
18+
*
19+
* A Voyage is identified by a unique voyage number.
20+
*
21+
* ---Annotations required by Doctrine---
22+
* @Entity(repositoryClass="Application\Infrastructure\Persistence\Doctrine\VoyageRepositoryDoctrine")
23+
* @Table(name="voyage")
24+
* --------------------------------------
25+
*
26+
* @author Alexander Miertsch <[email protected]>
27+
*/
28+
class Voyage implements EntityInterface
29+
{
30+
/**
31+
* Unique Identifier
32+
*
33+
* ---Annotations required by Doctrine---
34+
* @Id
35+
* @Column(type="voyagenumber", length=30, unique=true, nullable=false)
36+
* --------------------------------------
37+
*
38+
* @var VoyageNumber
39+
*/
40+
protected $voyageNumber;
41+
42+
/**
43+
* Name of the voyage
44+
*
45+
* ---Annotations required by Doctrine---
46+
* @Column(type="string", length=100)
47+
* --------------------------------------
48+
*
49+
* @var integer
50+
*/
51+
protected $name;
52+
53+
/**
54+
* Capacity of the Voyage
55+
*
56+
* ---Annotations required by Doctrine---
57+
* @Column(type="integer")
58+
* --------------------------------------
59+
*
60+
* @var integer
61+
*/
62+
protected $capacity;
63+
64+
/**
65+
* Construct
66+
*
67+
* @param VoyageNumber $voyageNumber
68+
*/
69+
public function __construct(VoyageNumber $voyageNumber)
70+
{
71+
$this->voyageNumber = $voyageNumber;
72+
}
73+
74+
/**
75+
* Get the unique id
76+
*
77+
* @return VoyageNumber
78+
*/
79+
public function getVoyageNumber()
80+
{
81+
return $this->voyageNumber;
82+
}
83+
84+
/**
85+
* Get name of the Voyage
86+
*
87+
* @return string
88+
*/
89+
public function getName()
90+
{
91+
return $this->name;
92+
}
93+
94+
/**
95+
* Get capacity of Voyage
96+
*
97+
* @return int
98+
*/
99+
public function getCapacity()
100+
{
101+
return $this->capacity;
102+
}
103+
104+
/**
105+
* Set name of Voyage
106+
*
107+
* @param string $name
108+
* @return void
109+
*/
110+
public function setName($name)
111+
{
112+
$this->name = $name;
113+
}
114+
115+
/**
116+
* Get capacity of Voyage
117+
*
118+
* @param integer $capacity
119+
* @return void
120+
*/
121+
public function setCapacity($capacity)
122+
{
123+
$this->capacity = $capacity;
124+
}
125+
126+
127+
/**
128+
* {@inheritDoc}
129+
*/
130+
public function sameIdentityAs(EntityInterface $other)
131+
{
132+
if (!$other instanceof Voyage) {
133+
return false;
134+
}
135+
136+
return $this->getVoyageNumber()->sameValueAs($other->getVoyageNumber());
137+
}
138+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Domain\Model\Voyage;
10+
11+
use Application\Domain\Shared\UID;
12+
use Application\Domain\Shared\ValueObjectInterface;
13+
/**
14+
* Unique identifier of a Voyage
15+
*
16+
* @author Alexander Miertsch <[email protected]>
17+
*/
18+
class VoyageNumber extends UID implements ValueObjectInterface
19+
{
20+
/**
21+
* {@inheritDoc}
22+
*/
23+
public function sameValueAs(ValueObjectInterface $other)
24+
{
25+
if (!$other instanceof VoyageNumber) {
26+
return false;
27+
}
28+
29+
return parent::sameValueAs($other);
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Domain\Model\Voyage;
10+
11+
/**
12+
* VoyageRepositoryInterface
13+
*
14+
* @author Alexander Miertsch <[email protected]>
15+
*/
16+
interface VoyageRepositoryInterface
17+
{
18+
/**
19+
* Find Voyage by number
20+
*
21+
* @param VoyageNumber $voyageNumber
22+
* @return Voyage
23+
*/
24+
public function findVoyage(VoyageNumber $voyageNumber);
25+
26+
/**
27+
* List all Voyages
28+
*
29+
* @return Voyage[]
30+
*/
31+
public function findAll();
32+
33+
/**
34+
* Saves given Voyage
35+
*
36+
* @param Voyage $voyage
37+
* @return void
38+
*/
39+
public function store(Voyage $voyage);
40+
}

module/Application/src/Application/Infrastructure/Persistence/Doctrine/Type/TrackingId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Doctrine\DBAL\Types\ConversionException;
1414
use Application\Domain\Model\Cargo\TrackingId as DomainTrackingId;
1515
/**
16-
* Custom Doctrine Type UID
16+
* Custom Doctrine Type TrackingId
1717
*
1818
* @author Alexander Miertsch <[email protected]>
1919
*/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Infrastructure\Persistence\Doctrine\Type;
10+
11+
use Doctrine\DBAL\Types\TextType;
12+
use Doctrine\DBAL\Platforms\AbstractPlatform;
13+
use Doctrine\DBAL\Types\ConversionException;
14+
use Application\Domain\Model\Voyage\VoyageNumber as DomainVoyageNumber;
15+
/**
16+
* Custom Doctrine Type VoyageNumber
17+
*
18+
* @author Alexander Miertsch <[email protected]>
19+
*/
20+
class VoyageNumber extends TextType
21+
{
22+
/**
23+
* {@inheritDoc}
24+
*/
25+
public function convertToPHPValue($value, AbstractPlatform $platform)
26+
{
27+
return new DomainVoyageNumber($value);
28+
}
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public function convertToDatabaseValue($value, AbstractPlatform $platform)
34+
{
35+
if (!$value instanceof DomainVoyageNumber) {
36+
throw ConversionException::conversionFailed($value, $this->getName());
37+
}
38+
39+
return $value->toString();
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Infrastructure\Persistence\Doctrine;
10+
11+
use Application\Domain\Model\Voyage;
12+
use Doctrine\ORM\EntityRepository;
13+
/**
14+
* Voyage Repository
15+
*
16+
* @author Alexander Miertsch <[email protected]>
17+
*/
18+
class VoyageRepositoryDoctrine extends EntityRepository implements Voyage\VoyageRepositoryInterface
19+
{
20+
/**
21+
* {@inheritDoc}
22+
*/
23+
public function findVoyage(Voyage\VoyageNumber $voyageNumber)
24+
{
25+
return $this->find($voyageNumber);
26+
}
27+
28+
/**
29+
* {@inheritDoc}
30+
*/
31+
public function store(Voyage\Voyage $voyage)
32+
{
33+
$this->getEntityManager()->persist($voyage);
34+
$this->getEntityManager()->flush($voyage);
35+
}
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace ApplicationTest\Domain\Model\Voyage;
10+
11+
use ApplicationTest\TestCase;
12+
use Application\Domain\Model\Voyage;
13+
/**
14+
* VoyageTest
15+
*
16+
* @author Alexander Miertsch <[email protected]>
17+
*/
18+
class VoyageTest extends TestCase
19+
{
20+
/**
21+
*
22+
* @var Voyage\Voyage
23+
*/
24+
protected $voyage;
25+
26+
protected function setUp()
27+
{
28+
$voyageNumber = new Voyage\VoyageNumber("SHIP123");
29+
$this->voyage = new Voyage\Voyage($voyageNumber);
30+
$this->voyage->setName('HongkongToHamburg');
31+
$this->voyage->setCapacity(100);
32+
}
33+
34+
public function testGetVoyageNumber()
35+
{
36+
$this->assertEquals('SHIP123', $this->voyage->getVoyageNumber()->toString());
37+
}
38+
39+
public function testGetName()
40+
{
41+
$this->assertEquals('HongkongToHamburg', $this->voyage->getName());
42+
}
43+
44+
public function testGetCapacity()
45+
{
46+
$this->assertEquals(100, $this->voyage->getCapacity());
47+
}
48+
49+
public function testSameIdentityAs()
50+
{
51+
$voyageNumber = new Voyage\VoyageNumber('SHIP123');
52+
53+
$checkVoyage = new Voyage\Voyage($voyageNumber);
54+
55+
$this->assertTrue($this->voyage->sameIdentityAs($checkVoyage));
56+
}
57+
}

0 commit comments

Comments
 (0)