Completed
Push — master ( 07146b...d456d7 )
by WEBEWEB
02:11
created

DataTablesManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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