PartResolverRegistry::getResolverForPart()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace JDR\Mailer;
4
5
use InvalidArgumentException;
6
use JDR\Mailer\Part\EmailPart;
7
use JDR\Mailer\Part\EmailPartResolver;
8
9
class PartResolverRegistry
10
{
11
    /**
12
     * @var EmailPartResolver[]
13
     */
14
    private $resolvers = [];
15
16
    /**
17
     * Add Resolver.
18
     *
19
     * @param EmailPartResolver $resolver
20
     * @param string $part
21
     *
22
     * @return self
23
     */
24 1
    public function addResolver(EmailPartResolver $resolver, $part): self
25
    {
26 1
        $this->resolvers[$part] = $resolver;
27
28 1
        return $this;
29
    }
30
31
    /**
32
     * Get resolver for given email part.
33
     *
34
     * @param EmailPart $part
35
     *
36
     * @return EmailPartResolver
37
     */
38 2
    public function getResolverForPart(EmailPart $part): EmailPartResolver
39
    {
40 2
        $identifier = get_class($part);
41
42 2
        if (!array_key_exists($identifier, $this->resolvers)) {
43 1
            throw new InvalidArgumentException(sprintf(
44 1
                'Resolver for the part "%s" does not exists. Please choose one of the following: %s',
45
                $identifier,
46 1
                implode(', ', array_keys($this->resolvers))
47
            ));
48
        }
49
50 1
        return $this->resolvers[$identifier];
51
    }
52
}
53