Completed
Push — master ( d64a29...6f20dd )
by Pol
02:49
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 13
  public function getPlugins() {
20 13
    $candidates = array_filter(
21 13
      ClassFinder::getClassesInNamespace('drupol\Yaroc\Plugin\Method'),
22 13
      function ($className) {
23 13
        return in_array('drupol\Yaroc\Plugin\MethodPluginInterface', class_implements($className));
24 13
      }
25
    );
26
27 13
    $classes = [];
28 13
    foreach ($candidates as $candidate) {
29 13
      $candidate = new \ReflectionClass($candidate);
30
31 13
      $classes[$candidate->getConstant('METHOD')] = $candidate;
32
    }
33
34 13
    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 13
  public function getPlugin($methodName, array $params = array()) {
48 13
    $plugins = $this->getPlugins();
49
50 13
    if (isset($plugins[$methodName])) {
51
      /** @var \drupol\Yaroc\Plugin\MethodPluginInterface $plugin */
52 13
      return $plugins[$methodName]->newInstance()->setParameters($params);
53
    }
54
55
    return FALSE;
56
  }
57
58
}
59