Passed
Push — main ( cd1997...8c175e )
by Sammy
01:28
created

Resolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
c 1
b 0
f 0
dl 0
loc 38
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolved() 0 7 2
A cascadeNamespace() 0 14 4
A isResolved() 0 3 1
A __construct() 0 2 1
1
<?php
2
3
namespace HexMakina\LeMarchand;
4
5
class Resolver
6
{
7
  private $namespace_cascade = [];
8
  private $resolved_cache = [];
9
10
  public function __construct($namespace_cascade = []){
11
    $this->namespace_cascade = $namespace_cascade;
12
  }
13
14
  public function cascadeNamespace($class_name)
15
  {
16
      if ($this->isResolved($class_name)) {
17
          return $this->resolved($class_name);
18
      }
19
20
      // not fully namespaced, lets cascade
21
      foreach ($this->namespace_cascade as $ns) {
22
          if (class_exists($fully_namespaced = $ns . $class_name)) {
23
              $this->resolved($class_name, $fully_namespaced);
24
              return $fully_namespaced;
25
          }
26
      }
27
      throw new NotFoundException($class_name);
28
  }
29
30
31
  public function resolved($clue, $solution = null)
32
  {
33
      if (!is_null($solution)) {
34
          $this->resolved_cache[$clue] = $solution;
35
      }
36
37
      return $this->resolved_cache[$clue] ?? null;
38
  }
39
40
  public function isResolved($clue): bool
41
  {
42
      return isset($this->resolved_cache[$clue]);
43
  }
44
}
45