Completed
Pull Request — master (#29)
by Mateusz
37:30 queued 34:33
created

ComposerPluginManager::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
crap 3
1
<?php
2
/**
3
 * MtMail - e-mail module for Zend Framework 2
4
 *
5
 * @link      http://github.com/mtymek/MtMail
6
 * @copyright Copyright (c) 2013-2014 Mateusz Tymek
7
 * @license   BSD 2-Clause
8
 */
9
10
namespace MtMail\Service;
11
12
use MtMail\ComposerPlugin\PluginInterface;
13
use MtMail\Exception\RuntimeException;
14
use Zend\ServiceManager\AbstractPluginManager;
15
use Zend\ServiceManager\Exception;
16
use Zend\ServiceManager\Exception\InvalidServiceException;
17
18
class ComposerPluginManager extends AbstractPluginManager
19
{
20
    protected $instanceOf = PluginInterface::class;
21
22
    /**
23
     * Validate the plugin is of the expected type (v3).
24
     *
25
     * Validates against `$instanceOf`.
26
     *
27
     * @param mixed $instance
28
     * @throws InvalidServiceException
29
     */
30
    public function validate($instance)
31
    {
32
        if (! $instance instanceof $this->instanceOf) {
33
            throw new InvalidServiceException(sprintf(
34
                '%s can only create instances of %s; %s is invalid',
35
                get_class($this),
36 3
                $this->instanceOf,
37
                (is_object($instance) ? get_class($instance) : gettype($instance))
38 3
            ));
39 1
        }
40 1
    }
41 1
42
    /**
43 1
     * Validate the plugin is of the expected type (v2).
44
     *
45 2
     * Proxies to `validate()`.
46
     *
47
     * @param mixed $instance
48
     * @throws RuntimeException
49
     */
50
    public function validatePlugin($instance)
51
    {
52
        try {
53 1
            $this->validate($instance);
54
        } catch (InvalidServiceException $e) {
55 1
            throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
56
        }
57
    }
58
59
    /**
60
     * Canonicalize name
61
     *
62
     * @param  string $name
63
     * @return string
64
     */
65
    protected function canonicalizeName($name)
66
    {
67
        return $name;
68
    }
69
}
70