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

findConsignmentService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
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