Passed
Pull Request — master (#39)
by Mariano
02:50
created

Phiremock::getPathFromCodeceptionDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * This file is part of phiremock-codeception-extension.
4
 *
5
 * phiremock-codeception-extension is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * phiremock-codeception-extension is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with phiremock-codeception-extension.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace Codeception\Extension;
20
21
use Codeception\Extension as CodeceptionExtension;
22
use Mcustiel\Phiremock\Codeception\Extension\Config;
23
use Mcustiel\Phiremock\Codeception\Extension\PhiremockProcessManager;
24
25
class Phiremock extends CodeceptionExtension
26
{
27
    /** @var array */
28
    public static $events = [
29
        'suite.before' => 'startProcess',
30
        'suite.after'  => 'stopProcess',
31
    ];
32
33
    /** @var array */
34
    protected $config = Config::DEFAULT_CONFIG;
35
36
    /** @var PhiremockProcessManager */
37
    private $process;
38
39
    /** @var Config */
40
    private $extensionConfig;
41
42
    public function __construct(
43
        array $config,
44
        array $options,
45
        PhiremockProcessManager $process = null
46
    ) {
47
        $this->setDefaultLogsPath();
48
        parent::__construct($config, $options);
49
        $this->extensionConfig = new Config($this->config);
50
        $this->initProcess($process);
51
    }
52
53
    public function startProcess(): void
54
    {
55
        $this->writeln('Starting default phiremock instance...');
56
        $this->process->start(
57
            $this->extensionConfig->getInterface(),
58
            $this->extensionConfig->getPort(),
0 ignored issues
show
Bug introduced by
$this->extensionConfig->getPort() of type string is incompatible with the type integer expected by parameter $port of Mcustiel\Phiremock\Codec...ProcessManager::start(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            /** @scrutinizer ignore-type */ $this->extensionConfig->getPort(),
Loading history...
59
            $this->extensionConfig->getPhiremockPath(),
60
            $this->extensionConfig->getLogsPath(),
61
            $this->extensionConfig->isDebugMode(),
62
            $this->extensionConfig->getExpectationsPath(),
63
            $this->extensionConfig->getServerFactory()
64
        );
65
        foreach ($this->extensionConfig->getExtraInstances() as $configInstance) {
66
            $this->writeln('Starting extra phiremock instance...');
67
            $this->process->start(
68
                $configInstance->getInterface(),
69
                $configInstance->getPort(),
70
                $configInstance->getPhiremockPath(),
71
                $configInstance->getLogsPath(),
72
                $configInstance->isDebugMode(),
73
                $configInstance->getExpectationsPath(),
74
                $configInstance->getServerFactory()
75
            );
76
        }
77
        $this->executeDelay();
78
    }
79
80
    public function stopProcess(): void
81
    {
82
        $this->writeln('Stopping phiremock...');
83
        $this->process->stop();
84
    }
85
86
    private function executeDelay(): void
87
    {
88
        $delay = $this->extensionConfig->getDelay();
89
        if ($delay > 0) {
90
            sleep($delay);
91
        }
92
    }
93
94
    private function initProcess(?PhiremockProcessManager $process): void
95
    {
96
        $this->process = $process ?? new PhiremockProcessManager();
97
    }
98
99
    private function setDefaultLogsPath(): void
100
    {
101
        $this->config['logs_path'] = Config::getDefaultLogsPath();
102
    }
103
}
104