Completed
Push — master ( 15d87d...7defa9 )
by WEBEWEB
14:36
created

DataTablesManager::getProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 WBW\Bundle\CoreBundle\Manager\AbstractManager;
15
use WBW\Bundle\CoreBundle\Manager\ManagerInterface;
16
use WBW\Bundle\JQuery\DataTablesBundle\Exception\AlreadyRegisteredDataTablesProviderException;
17
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException;
18
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
19
20
/**
21
 * DataTables manager.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\JQuery\DataTablesBundle\Manager
25
 */
26
class DataTablesManager extends AbstractManager {
27
28
    /**
29
     * Service name.
30
     *
31
     * @var string
32
     */
33
    const SERVICE_NAME = "webeweb.jquery_datatables.manager";
34
35
    /**
36
     * Index.
37
     *
38
     * @var array
39
     */
40
    private $index;
41
42
    /**
43
     * Constructor.
44
     */
45
    public function __construct() {
46
        parent::__construct();
47
        $this->setIndex([]);
48
    }
49
50
    /**
51
     * Determines if a provider exists.
52
     *
53
     * @param string $name The name.
54
     * @return bool Returns true in case of success, false otherwise.
55
     */
56
    protected function exists($name) {
57
        return array_key_exists($name, $this->index);
58
    }
59
60
    /**
61
     * Get the index.
62
     *
63
     * @return array Returns the index.
64
     */
65
    public function getIndex() {
66
        return $this->index;
67
    }
68
69
    /**
70
     * Get a provider.
71
     *
72
     * @param string $name The name.
73
     * @return DataTablesProviderInterface Returns the provider.
74
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
75
     */
76
    public function getProvider($name) {
77
        if (false === $this->exists($name)) {
78
            throw new UnregisteredDataTablesProviderException($name);
79
        }
80
        return $this->getProviders()[$this->index[$name]];
81
    }
82
83
    /**
84
     * Register a provider.
85
     *
86
     * @param DataTablesProviderInterface $provider The provider.
87
     * @return ManagerInterface Returns this manager.
88
     * @throws AlreadyRegisteredDataTablesProviderException Throws an already registered provider exception.
89
     */
90
    public function registerProvider(DataTablesProviderInterface $provider) {
91
        if (true === $this->exists($provider->getName())) {
92
            throw new AlreadyRegisteredDataTablesProviderException($provider->getName());
93
        }
94
        $this->index[$provider->getName()] = $this->size();
95
        return $this->addProvider($provider);
96
    }
97
98
    /**
99
     * Set the index.
100
     *
101
     * @param array $index The index.
102
     * @return ManagerInterface Returns this manager.
103
     */
104
    protected function setIndex(array $index) {
105
        $this->index = $index;
106
        return $this;
107
    }
108
}
109