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 Throwable; |
||
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 | use WBW\Library\Symfony\Manager\AbstractManager; |
||
20 | use WBW\Library\Symfony\Manager\ManagerInterface; |
||
21 | use WBW\Library\Symfony\Provider\ProviderInterface; |
||
22 | |||
23 | /** |
||
24 | * DataTables manager. |
||
25 | * |
||
26 | * @author webeweb <https://github.com/webeweb> |
||
27 | * @package WBW\Bundle\JQuery\DataTablesBundle\Manager |
||
28 | */ |
||
29 | class DataTablesManager extends AbstractManager { |
||
30 | |||
31 | /** |
||
32 | * Service name. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | const SERVICE_NAME = "wbw.jquery.datatables.manager"; |
||
37 | |||
38 | /** |
||
39 | * {@inheritDoc} |
||
40 | * @throws Throwable Throws an exception if an error occurs. |
||
41 | */ |
||
42 | public function addProvider(ProviderInterface $provider): ManagerInterface { |
||
43 | |||
44 | if (false === ($provider instanceof DataTablesProviderInterface)) { |
||
45 | throw new InvalidArgumentException("The provider must implements " . DataTablesProviderInterface::class); |
||
46 | } |
||
47 | |||
48 | /** @var DataTablesProviderInterface $provider */ |
||
49 | if (true === $this->containsProvider($provider)) { |
||
50 | throw new AlreadyRegisteredDataTablesProviderException($provider->getName()); |
||
51 | } |
||
52 | |||
53 | return parent::addProvider($provider); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get a provider. |
||
58 | * |
||
59 | * @param string $name The name. |
||
60 | * @return DataTablesProviderInterface Returns the provider. |
||
61 | * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
||
62 | */ |
||
63 | public function getProvider(string $name): DataTablesProviderInterface { |
||
64 | |||
65 | /** @var DataTablesProviderInterface $current */ |
||
66 | foreach ($this->getProviders() as $current) { |
||
67 | if ($name === $current->getName()) { |
||
68 | return $current; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
69 | } |
||
70 | } |
||
71 | |||
72 | throw new UnregisteredDataTablesProviderException($name); |
||
73 | } |
||
74 | } |
||
75 |