Application   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 76
wmc 7
lcom 1
cbo 4
ccs 21
cts 21
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A handle() 0 18 3
1
<?php
2
namespace Skansing\Escapology\Router;
3
4
use \Skansing\Escapology\Cacher,
5
    \Skansing\Escapology\Dispatcher,
6
    \Skansing\Escapology\Router,
7
    \Skansing\Escapology\RouteFileParser,
8
    \Skansing\Escapology\RouteFileParser\Regex as RegexRouteFileParser,
9
    \Skansing\Escapology\Dispatcher\Regex as RegexDispatcher;
10
  
11
class Application implements Router {
12
 
13
    /**
14
     * @var RouteFileParser $routeFileParser
15
     */
16
    private $routeFileParser;
17
18
    /**
19
     * @var Dispatcher $dispatcher
20
     */
21
    private $dispatcher;
22
23
    /**
24
     * @var Cacher $cacher
25
     */
26
    private $cacher;
27
28
    /**
29
     * @var string $cacheKey]
30
     */
31
    private $cacheKey;
32
33
    /**
34
     * @var boolean $cacheInUse
35
     */
36
    private $cacheInUse = false;
37
38
  /**
39
   * Application router
40
   *
41
   * Routes found routes to the new application and not found to the old
42
   *
43
   * @param Dispatcher|null      $dispatcher      
44
   * @param RouteFileParser|null $routeFileParser
45
   * @param Cacher $cacher
46
   * @param string $cacheKey
47
   */
48 7
  public function __construct(
49
    Dispatcher $dispatcher,  
50
    RouteFileParser $routeFileParser = null,
51
    Cacher $cacher = null,
52
    $cacheKey = null 
53
  ){
54 7
    $this->routeFileParser = $routeFileParser ?: new RegexRouteFileParser;
55 7
    $this->dispatcher = $dispatcher;
56 7
    $this->cacher = $cacher;
57 7
    if($this->cacher) {
58 3
      $this->cacheKey = $cacheKey ?: '__routeCache';  
59 3
      $this->cacheInUse = true;
60 3
    }
61 7
  }
62
63
  /**
64
   * Routes to new or old application
65
   * 
66
   * @param  string $routesFile     
67
   */
68 4
  public function handle(
69
    $routesFile
70
  ){
71 4
    if($this->cacheInUse) {
72 2
      $routeData = $this->cacher->get($this->cacheKey);  
73 2
      if($routeData === false) {
74 1
        $routeData = $this->routeFileParser->digest($routesFile);
75 1
        $this->cacher->set($this->cacheKey, $routeData);
76 1
      }  
77 2
    } else {
78 2
      $routeData = $this->routeFileParser->digest($routesFile);
79
    }
80 4
    $result = $this->dispatcher->dispatch(
81
      $routeData
82 4
    );
83
84 4
    return $result;
85
  }
86
}
87