Completed
Push — master ( 70e4e5...5530b2 )
by Joachim
15:31
created

ConsignmentServiceCollection   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 38
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addConsignmentService() 0 5 1
A findConsignmentService() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\DandomainConsignmentBundle\ConsignmentService;
6
7
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentConsignmentServiceException;
8
use Loevgaard\DandomainFoundation\Entity\Generated\ManufacturerInterface;
9
use Symfony\Component\DependencyInjection\Container;
10
11
class ConsignmentServiceCollection
12
{
13
    /**
14
     * @var ConsignmentServiceInterface[]
15
     */
16
    protected $consignmentServices;
17
18
    public function __construct()
19
    {
20
        $this->consignmentServices = [];
21
    }
22
23
    public function addConsignmentService(ConsignmentServiceInterface $consignmentService) : void
24
    {
25
        $class = (new \ReflectionClass($consignmentService))->getShortName();
26
        $this->consignmentServices[$class] = $consignmentService;
27
    }
28
29
    /**
30
     * @param ManufacturerInterface $manufacturer
31
     * @return ConsignmentServiceInterface
32
     * @throws NonExistentConsignmentServiceException
33
     */
34
    public function findConsignmentService(ManufacturerInterface $manufacturer) : ConsignmentServiceInterface
35
    {
36
        $name = Container::camelize($manufacturer->getName()).'ConsignmentService';
37
38
        if(!isset($this->consignmentServices[$name])) {
39
            throw new NonExistentConsignmentServiceException('Consignment service `'.$name.'` not found. Did you mean any of these? '.join(', ', array_keys($this->consignmentServices)));
40
        }
41
42
        /** @var ConsignmentServiceInterface $service */
43
        $service = $this->consignmentServices[$name];
44
        $service->setManufacturer($manufacturer);
45
46
        return $service;
47
    }
48
}
49