Passed
Push — master ( 8a0955...800a4a )
by WEBEWEB
03:03
created

DataTablesManager::addProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 8
c 3
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\JQuery\DataTablesBundle\Manager;
13
14
use InvalidArgumentException;
15
use WBW\Bundle\JQuery\DataTablesBundle\Exception\AlreadyRegisteredDataTablesProviderException;
16
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException;
17
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
18
use WBW\Library\Symfony\Manager\AbstractManager;
19
use WBW\Library\Symfony\Manager\ManagerInterface;
20
use WBW\Library\Symfony\Provider\ProviderInterface;
21
22
/**
23
 * DataTables manager.
24
 *
25
 * @author webeweb <https://github.com/webeweb>
26
 * @package WBW\Bundle\JQuery\DataTablesBundle\Manager
27
 */
28
class DataTablesManager extends AbstractManager {
29
30
    /**
31
     * Service name.
32
     *
33
     * @var string
34
     */
35
    const SERVICE_NAME = "wbw.jquery.datatables.manager";
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function addProvider(ProviderInterface $provider): ManagerInterface {
41
42
        /** @var DataTablesProviderInterface $provider */
43
        if (true === $this->containsProvider($provider)) {
44
            throw new AlreadyRegisteredDataTablesProviderException($provider->getName());
45
        }
46
47
        return parent::addProvider($provider);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function containsProvider(ProviderInterface $provider): bool {
54
55
        /** @var DataTablesProviderInterface $provider */
56
        if (false === ($provider instanceof DataTablesProviderInterface)) {
57
            throw new InvalidArgumentException("The provider must implements " . DataTablesProviderInterface::class);
58
        }
59
60
        /** @var DataTablesProviderInterface $current */
61
        foreach ($this->getProviders() as $current) {
62
            if ($provider->getName() === $current->getName()) {
0 ignored issues
show
Bug introduced by
The method getName() does not exist on WBW\Library\Symfony\Provider\ProviderInterface. It seems like you code against a sub-type of WBW\Library\Symfony\Provider\ProviderInterface such as WBW\Bundle\CoreBundle\Pr...AssetsProviderInterface or WBW\Library\Symfony\Prov...\ColorProviderInterface or WBW\Bundle\CoreBundle\Pr...eletonProviderInterface or WBW\Bundle\JQuery\DataTa...TablesProviderInterface or WBW\Bundle\JQuery\DataTa...loyeeDataTablesProvider or WBW\Bundle\JQuery\DataTa...loyeeDataTablesProvider or WBW\Library\Symfony\Prov...nThemeProviderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            if ($provider->getName() === $current->/** @scrutinizer ignore-call */ getName()) {
Loading history...
63
                return true;
64
            }
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * Get a provider.
72
     *
73
     * @param string $name The name.
74
     * @return DataTablesProviderInterface Returns the provider.
75
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
76
     */
77
    public function getProvider(string $name): DataTablesProviderInterface {
78
79
        /** @var DataTablesProviderInterface $current */
80
        foreach ($this->getProviders() as $current) {
81
            if ($name === $current->getName()) {
82
                return $current;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $current returns the type WBW\Library\Symfony\Provider\ProviderInterface which includes types incompatible with the type-hinted return WBW\Bundle\JQuery\DataTa...TablesProviderInterface.
Loading history...
83
            }
84
        }
85
86
        throw new UnregisteredDataTablesProviderException($name);
87
    }
88
}
89