Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
41.18% |
7 / 17 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
factory | |
41.18% |
7 / 17 |
|
50.00% |
2 / 4 |
16.97 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
get_instance | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
garbage_collect | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
get_captcha_types | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\captcha; |
15 | |
16 | use phpbb\captcha\plugins\legacy_wrapper; |
17 | use phpbb\captcha\plugins\plugin_interface; |
18 | |
19 | class factory |
20 | { |
21 | /** |
22 | * @var \Symfony\Component\DependencyInjection\ContainerInterface |
23 | */ |
24 | private $container; |
25 | |
26 | /** |
27 | * @var \phpbb\di\service_collection |
28 | */ |
29 | private $plugins; |
30 | |
31 | /** |
32 | * Constructor |
33 | * |
34 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container |
35 | * @param \phpbb\di\service_collection $plugins |
36 | */ |
37 | public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, \phpbb\di\service_collection $plugins) |
38 | { |
39 | $this->container = $container; |
40 | $this->plugins = $plugins; |
41 | } |
42 | |
43 | /** |
44 | * Return a new instance of a given plugin |
45 | * |
46 | * @param $name |
47 | * @return plugin_interface |
48 | */ |
49 | public function get_instance($name): plugin_interface |
50 | { |
51 | $captcha = $this->container->get($name); |
52 | if ($captcha instanceof plugin_interface) |
53 | { |
54 | return $captcha; |
55 | } |
56 | |
57 | return new legacy_wrapper($captcha); |
58 | } |
59 | |
60 | /** |
61 | * Call the garbage collector |
62 | * |
63 | * @param string $name The name to the captcha service. |
64 | */ |
65 | function garbage_collect($name) |
66 | { |
67 | $captcha = $this->get_instance($name); |
68 | $captcha->garbage_collect(); |
69 | } |
70 | |
71 | /** |
72 | * Return a list of all registered CAPTCHA plugins |
73 | * |
74 | * @returns array |
75 | */ |
76 | function get_captcha_types() |
77 | { |
78 | $captchas = array( |
79 | 'available' => array(), |
80 | 'unavailable' => array(), |
81 | ); |
82 | |
83 | foreach ($this->plugins as $plugin => $plugin_instance) |
84 | { |
85 | if ($plugin_instance->is_available()) |
86 | { |
87 | $captchas['available'][$plugin] = $plugin_instance->get_name(); |
88 | } |
89 | else |
90 | { |
91 | $captchas['unavailable'][$plugin] = $plugin_instance->get_name(); |
92 | } |
93 | } |
94 | |
95 | return $captchas; |
96 | } |
97 | } |