Completed
Push — work-fleets ( b30f48...88e4e3 )
by SuperNova.WS
08:13
created

Accessors::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Created by Gorlum 19.08.2016 20:35
4
 */
5
6
namespace Common;
7
8
/**
9
 * Accessors storage
10
 *
11
 * TODO - make magic method access to accessors ????????
12
 *
13
 * @package Common
14
 */
15
class Accessors {
16
17
  /**
18
   * Array of accessors - getters/setters/etc
19
   *
20
   * @var callable[]
21
   */
22
  protected $accessors = array();
23
24
  /**
25
   * @var bool[]
26
   */
27
  protected $shared = array();
28
29
  /**
30
   * Result of shared function execution
31
   *
32
   * @var array
33
   */
34
  protected $executed = array();
35
36
  /**
37
   * @param string $accessor
38
   * @param string $varName
39
   *
40
   * @return bool
41
   */
42
  public function exists($accessor, $varName) {
43
    return isset($this->accessors[$accessor . $varName]);
44
  }
45
46
  /**
47
   * Assign accessor to a named variable
48
   *
49
   * Different accessors have different signatures - you should look carefully before assigning accessor
50
   *
51
   * @param string   $accessor - type of accessor getter/setter/importer/exporter/etc
52
   * @param string   $varName
53
   * @param callable $callable
54
   *
55
   * @param bool     $shared
56
   *
57
   * @throws \Exception
58
   */
59
  public function set($accessor, $varName, $callable, $shared = false) {
60
    if (empty($callable)) {
61
      return;
62
    } elseif (!is_callable($callable)) {
63
      throw new \Exception('Error assigning callable in ' . get_called_class() . '::set()! Callable typed [' . $accessor . '] is not a callable or not accessible in the scope');
64
    }
65
66
    // Converting method array-callable to closure
67
68
//    // This commented code require PHP 5.4+ !!!!!!!!!!
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
//    if (is_array($callable) && count($callable) == 2 && is_object($callable[0])) {
70
//      $method = new \ReflectionMethod($callable[0], $callable[1]);
71
//      $callable = $method->getClosure($callable[0]);
72
//    }
73
    if(is_array($callable) && ($invoker = Invoker::build($callable))) {
74
      $callable = $invoker;
75
    }
76
77
    $functionName = $accessor . $varName;
78
    $this->accessors[$functionName] = $callable;
79
    if($shared) {
80
      $this->shared[$functionName] = true;
81
    }
82
  }
83
84
  /**
85
   * Gets accessor for later use
86
   *
87
   * @param string $accessor
88
   *
89
   * @param string $varName
90
   *
91
   * @return callable|null
92
   */
93
  public function get($accessor, $varName) {
94
    return $this->exists($accessor, $varName) ? $this->accessors[$accessor . $varName] : null;
95
  }
96
97
  /**
98
   * @param string $accessor
99
   * @param string $varName
100
   * @param array  $params
101
   *
102
   * @return mixed
103
   * @throws \Exception
104
   */
105
  public function execute($accessor, $varName, $params) {
106
    if (!$this->exists($accessor, $varName)) {
107
      throw new \Exception("No [{$accessor}] accessor found for variable [{$varName}] on " . get_called_class() . "::" . __METHOD__);
108
    }
109
110
    $functionName = $accessor . $varName;
111
    if(!isset($this->shared[$functionName]) || !array_key_exists($functionName, $this->executed)) {
112
      $this->executed[$functionName] = call_user_func_array($this->accessors[$functionName], $params);
113
    }
114
115
    return $this->executed[$functionName];
116
  }
117
118
}
119