Skip to content

Commit

Permalink
Add Network mapping to its services (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeparaujo authored Sep 19, 2017
1 parent dd30be9 commit 959dfff
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 88 deletions.
6 changes: 6 additions & 0 deletions src/Data/ProgrammesDb/Entity/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class Network
*/
private $endDate;

/**
* Used for joins. Cannot be queried, so there is no getter/setter.
* @ORM\OneToMany(targetEntity="Service", mappedBy="network")
*/
private $services;

public function __construct(string $nid, string $name)
{
$this->nid = $nid;
Expand Down
2 changes: 1 addition & 1 deletion src/Data/ProgrammesDb/Entity/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Service
/**
* @var Network|null
*
* @ORM\ManyToOne(targetEntity="Network", cascade={"persist"})
* @ORM\ManyToOne(targetEntity="Network", inversedBy="services", cascade={"persist"})
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/
private $network;
Expand Down
2 changes: 1 addition & 1 deletion src/Data/ProgrammesDb/Entity/Traits/OptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
trait OptionsTrait
{
/**
* @var array
* @var array|null
*
* @ORM\Column(type="json_array", nullable=true)
*/
Expand Down
14 changes: 13 additions & 1 deletion src/Data/ProgrammesDb/EntityRepository/ServiceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ public function findByIds(array $ids): array
->addSelect(['masterBrand', 'network'])
->leftJoin('service.masterBrand', 'masterBrand')
->leftJoin('service.network', 'network')
->andWhere("service.id IN(:ids)")
->andWhere("service.id IN (:ids)")
->setParameter('ids', $ids)
->getQuery()->getResult(Query::HYDRATE_ARRAY);
}

public function findByIdsWithNetworkServicesList(array $ids): array
{
return $this->createQueryBuilder('service')
->addSelect(['masterBrand', 'network', 'networkServices'])
->leftJoin('service.masterBrand', 'masterBrand')
->leftJoin('service.network', 'network')
->leftJoin('network.services', 'networkServices')
->andWhere("service.id IN (:ids)")
->setParameter('ids', $ids)
->getQuery()->getResult(Query::HYDRATE_ARRAY);
}
Expand Down
34 changes: 33 additions & 1 deletion src/Domain/Entity/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class Network
/** @var Options */
private $options;

/** @var Service[]|null */
private $services;

public function __construct(
Nid $nid,
string $name,
Expand All @@ -63,8 +66,10 @@ public function __construct(
bool $isChildrens = false,
bool $isWorldServiceInternational = false,
bool $isInternational = false,
bool $isAllowedAdverts = false
bool $isAllowedAdverts = false,
?array $services = null
) {
// Validate Medium
if (!in_array($medium, NetworkMediumEnum::validValues(), true)) {
throw new InvalidArgumentException(sprintf(
'$medium has an invalid value. Expected one of %s but got "%s"',
Expand All @@ -73,6 +78,18 @@ public function __construct(
));
}

// Validate array of Services
if (!is_null($services)) {
foreach ($services as $service) {
if (!$service instanceof Service) {
throw new InvalidArgumentException(sprintf(
'Expected an array of Services but got %s',
(is_object($service) ? get_class($service) : gettype($service))
));
}
}
}

$this->nid = $nid;
$this->name = $name;
$this->image = $image;
Expand All @@ -86,6 +103,7 @@ public function __construct(
$this->isWorldServiceInternational = $isWorldServiceInternational;
$this->isInternational = $isInternational;
$this->isAllowedAdverts = $isAllowedAdverts;
$this->services = $services;
}

public function getNid(): Nid
Expand Down Expand Up @@ -186,4 +204,18 @@ public function getOption(string $key)
{
return $this->options->getOption($key);
}

/**
* @throws DataNotFetchedException
*/
public function getServices(): array
{
if (is_null($this->services)) {
throw new DataNotFetchedException(
'Could not get Services of Network "' . $this->nid . '" as they were not fetched'
);
}

return $this->services;
}
}
21 changes: 20 additions & 1 deletion src/Mapper/ProgrammesDbToDomain/NetworkMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public function getCacheKey(array $dbNetwork): string
return $this->buildCacheKey($dbNetwork, 'id', [
'image' => 'Image',
'defaultService' => 'Service',
], [
'services' => 'Service',
]);
}

Expand All @@ -39,7 +41,8 @@ public function getDomainModel(array $dbNetwork): Network
$dbNetwork['isChildrens'],
$dbNetwork['isWorldServiceInternational'],
$dbNetwork['isInternational'],
$dbNetwork['isAllowedAdverts']
$dbNetwork['isAllowedAdverts'],
$this->getServiceModels($dbNetwork)
);
}

Expand Down Expand Up @@ -69,6 +72,22 @@ private function getServiceModel(array $dbNetwork, string $key = 'defaultService
return $this->mapperFactory->getServiceMapper()->getDomainModel($dbNetwork[$key]);
}

private function getServiceModels(array $dbProgramme, string $key = 'services'): ?array
{
if (!isset($dbProgramme[$key]) || !is_array($dbProgramme[$key])) {
return null;
}

$serviceMapper = $this->mapperFactory->getServiceMapper();
$services = [];

foreach ($dbProgramme[$key] as $dbService) {
$services[] = $serviceMapper->getDomainModel($dbService);
}

return $services;
}

private function getOptionsModel(array $dbNetwork, string $key = 'options'): Options
{
// Networks have no parents so this is simple. They don't care nor process any other options
Expand Down
Loading

0 comments on commit 959dfff

Please sign in to comment.