Completed
Push — master ( a33098...401d22 )
by Johan de
03:55
created

PartResolverRegistry::addResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php declare(strict_types = 1);
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 1.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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