Completed
Push — master ( 60b2be...f29513 )
by Pol
06:10
created

MethodPluginManager::getPlugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2.032
1
<?php
2
3
namespace drupol\Yaroc\Plugin;
4
5
use drupol\Yaroc\Utilities\ClassFinder;
6
7
/**
8
 * Class MethodPluginManager.
9
 *
10
 * @package drupol\Yaroc\Plugin
11
 */
12
class MethodPluginManager {
13
14
  /**
15
   * Get the Method plugins.
16
   *
17
   * @return array
18
   */
19 12
  public function getPlugins() {
20 12
    $candidates = array_filter(
21 12
      ClassFinder::getClassesInNamespace('drupol\Yaroc\Plugin\Method'),
22 12
      function ($className) {
23 12
        return in_array('drupol\Yaroc\Plugin\MethodPluginInterface', class_implements($className));
24
      }
25 12
    );
26
27 12
    $classes = [];
28 12
    foreach ($candidates as $candidate) {
29 12
      $candidate = new \ReflectionClass($candidate);
30
31 12
      $classes[$candidate->getConstant('METHOD')] = $candidate;
32 12
    }
33
34 12
    return $classes;
35
  }
36
37
  /**
38
   * Get a specific Method plugin.
39
   *
40
   * @param string $methodName
41
   *   The method name to call.
42
   * @param array $params
43
   *   The params of the plugin.
44
   *
45
   * @return MethodPluginInterface|bool
46
   */
47 12
  public function getPlugin($methodName, array $params = array()) {
48 12
    $plugins = $this->getPlugins();
49
50 12
    if (isset($plugins[$methodName])) {
51
      /** @var \drupol\Yaroc\Plugin\MethodPluginInterface $plugin */
52 12
      return $plugins[$methodName]->newInstance()->setParameters($params);
53
    }
54
55
    return FALSE;
56
  }
57
58
}
59