Passed
Pull Request — master (#1)
by ANTHONIUS
01:35
created

WithConfigurationFile::loadPipelineFromFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 1
1
<?php
2
3
/*
4
 * This file is part of the doyo/mezzio-testing project.
5
 *
6
 * (c) Anthonius Munthi <https://itstoni.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\Mezzio\Testing\Modules;
15
16
use Doyo\Mezzio\Testing\Exception\WithConfigurationException;
17
use Laminas\ConfigAggregator\ArrayProvider;
18
use Psr\Container\ContainerInterface;
19
20
trait WithConfigurationFile
21
{
22
    protected static ?string $configDir = null;
23
24
    protected static string $configFilename    = 'config.php';
25
    protected static string $containerFilename = 'container.php';
26
    protected static string $pipelineFilename  = 'pipeline.php';
27
    protected static string $routesFilename    = 'routes.php';
28
29
    abstract protected function setupConfigFiles();
30
31
    /**
32
     * @throws WithConfigurationException when config directory not exists
33
     * @throws WithConfigurationException when configuration file not exists
34
     */
35
    final protected static function loadConfigFromFile(): void
36
    {
37
        $configDir = static::$configDir;
38
39
        if ( ! is_dir($configDir)) {
40
            throw WithConfigurationException::configDirNotExists($configDir);
0 ignored issues
show
Bug introduced by
It seems like $configDir can also be of type null; however, parameter $path of Doyo\Mezzio\Testing\Exce...n::configDirNotExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

40
            throw WithConfigurationException::configDirNotExists(/** @scrutinizer ignore-type */ $configDir);
Loading history...
41
        }
42
43
        $configFile = $configDir.'/'.static::$configFilename;
44
        if ( ! is_file($configFile) || ! is_readable($configFile)) {
45
            throw WithConfigurationException::configFileNotExists($configDir, static::$configFilename);
0 ignored issues
show
Bug introduced by
It seems like $configDir can also be of type null; however, parameter $dir of Doyo\Mezzio\Testing\Exce...::configFileNotExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

45
            throw WithConfigurationException::configFileNotExists(/** @scrutinizer ignore-type */ $configDir, static::$configFilename);
Loading history...
46
        }
47
48
        // reset default config providers
49
        static::$configProviders = [];
0 ignored issues
show
Bug Best Practice introduced by
The property configProviders does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
        $configs                 = require $configFile;
51
        $provider                = new ArrayProvider($configs);
52
        static::addConfigProvider($provider);
53
    }
54
55
    /**
56
     * @throws WithConfigurationException @throws when container configuration not exists
57
     *
58
     * @return ContainerInterface
59
     */
60
    final protected static function loadContainerFromFile(): ContainerInterface
61
    {
62
        $containerFile = static::$configDir.'/'.static::$containerFilename;
63
64
        if ( ! is_file($containerFile) || ! is_readable($containerFile)) {
65
            throw WithConfigurationException::containerFileNotExist(static::$configDir, static::$containerFilename);
0 ignored issues
show
Bug introduced by
It seems like static::configDir can also be of type null; however, parameter $dir of Doyo\Mezzio\Testing\Exce...containerFileNotExist() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

65
            throw WithConfigurationException::containerFileNotExist(/** @scrutinizer ignore-type */ static::$configDir, static::$containerFilename);
Loading history...
66
        }
67
68
        return require $containerFile;
69
    }
70
71
    /**
72
     * @throws WithConfigurationException when pipeline configuration not exists
73
     */
74
    final protected static function loadPipelineFromFile(): callable
75
    {
76
        $pipelineFile =  static::$configDir.'/'.static::$pipelineFilename;
77
78
        if ( ! is_file($pipelineFile) || ! is_readable($pipelineFile)) {
79
            throw WithConfigurationException::pipelineFileNotExists(static::$configDir, static::$pipelineFilename);
0 ignored issues
show
Bug introduced by
It seems like static::configDir can also be of type null; however, parameter $dir of Doyo\Mezzio\Testing\Exce...pipelineFileNotExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

79
            throw WithConfigurationException::pipelineFileNotExists(/** @scrutinizer ignore-type */ static::$configDir, static::$pipelineFilename);
Loading history...
80
        }
81
82
        return require $pipelineFile;
83
    }
84
85
    /**
86
     * @throws WithConfigurationException when routes configuration file not exists
87
     */
88
    final protected static function loadRoutesFromFile(): callable
89
    {
90
        $routesFile = static::$configDir.'/'.static::$routesFilename;
91
92
        if ( ! is_file($routesFile) || ! is_readable($routesFile)) {
93
            throw WithConfigurationException::routesFileNotExists(static::$configDir, static::$routesFilename);
0 ignored issues
show
Bug introduced by
It seems like static::configDir can also be of type null; however, parameter $dir of Doyo\Mezzio\Testing\Exce...::routesFileNotExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

93
            throw WithConfigurationException::routesFileNotExists(/** @scrutinizer ignore-type */ static::$configDir, static::$routesFilename);
Loading history...
94
        }
95
96
        return require $routesFile;
97
    }
98
}
99