Completed
Push — master ( 9763f8...278b18 )
by Westin
16:38 queued 06:42
created

MiddlewareSetupRunner::setPipelineFilePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\SwooleExpressive\Bridge;
5
6
use Psr\Container\ContainerInterface;
7
use WShafer\SwooleExpressive\Exception\MissingPipeLineException;
8
use WShafer\SwooleExpressive\Exception\MissingRoutesException;
9
use Zend\Expressive\Application;
10
use Zend\Expressive\MiddlewareFactory;
11
12
class MiddlewareSetupRunner implements MiddlewareSetupRunnerInterface
13
{
14
    /** @var Application */
15
    protected $application;
16
17
    /** @var MiddlewareFactory */
18
    protected $factory;
19
20
    /** @var ContainerInterface */
21
    protected $container;
22
23
    protected $pipelineFile =  __DIR__ . '/../../../../../config/pipeline.php';
24
25
    protected $routeFile = __DIR__ . '/../../../../../config/routes.php';
26
27
    public function __construct(
28
        Application $application,
29
        MiddlewareFactory $factory,
30
        ContainerInterface $container
31
    ) {
32
        $this->application = $application;
33
        $this->factory = $factory;
34
        $this->container = $container;
35
    }
36
37
    /**
38
     * @return bool
39
     * @throws MissingPipeLineException
40
     * @throws MissingRoutesException
41
     */
42
    public function execute() : bool
43
    {
44
        $pipeLine = $this->getPipeline();
45
46
        if (!$pipeLine) {
47
            throw new MissingPipeLineException('Cannot locate pipeline');
48
        }
49
50
        $routes = $this->getRoutes();
51
52
        if (!$routes) {
53
            throw new MissingRoutesException('Cannot locate routes');
54
        }
55
56
        // Execute programmatic/declarative middleware pipeline and routing
57
        // configuration statements
58
59
        $pipeLineRunner = require $pipeLine;
60
        $pipeLineRunner($this->application, $this->factory, $this->container);
61
62
        $routeRunner = require $routes;
63
        $routeRunner($this->application, $this->factory, $this->container);
64
65
        return true;
66
    }
67
68
    public function getPipeline()
69
    {
70
        if (file_exists($this->pipelineFile)) {
71
            return $this->pipelineFile;
72
        }
73
74
        return null;
75
    }
76
77
    public function getRoutes()
78
    {
79
        if (file_exists($this->routeFile)) {
80
            return $this->routeFile;
81
        }
82
83
        return null;
84
    }
85
86
    /**
87
     * @param $path
88
     * @throws MissingPipeLineException
89
     */
90
    public function setPipelineFilePath($path)
91
    {
92
        if (!file_exists($path)) {
93
            throw new MissingPipeLineException($path.' does not exist or is not readable');
94
        }
95
96
        $this->pipelineFile = $path;
97
    }
98
99
    /**
100
     * @param $path
101
     * @throws MissingRoutesException
102
     */
103
    public function setRoutesFilePath($path)
104
    {
105
        if (!file_exists($path)) {
106
            throw new MissingRoutesException($path.' does not exist or is not readable');
107
        }
108
109
        $this->routeFile = $path;
110
    }
111
}
112