Passed
Push — 0.7.0 ( 7cf8ff...8cbd21 )
by Alexander
04:15 queued 11s
created

BootDetectEnvironment::setEnvironmentFilePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 * @since       0.1.2
22
 */
23
24
namespace Syscodes\Core\Bootstrap;
25
26
use Exception;
27
use Syscodes\Dotenv\Dotenv;
28
use Syscodes\Contracts\Core\Application;
29
use Syscodes\Dotenv\Repository\RepositoryCreator;
30
31
/**
32
 * Initialize boot of a ParseEnv instance.
33
 * 
34
 * @author Alexander Campo <[email protected]>
35
 */
36
class BootDetectEnvironment
37
{
38
    /**
39
     * The application implementation.
40
     *
41
     * @var \Syscodes\Contracts\Core\Application $app
42
     */
43
    protected $app;
44
    
45
    /**
46
     * Bootstrap the given application.
47
     *
48
     * @param  \Syscodes\Contracts\Core\Application  $app
49
     * 
50
     * @return void
51
     */
52
    public function bootstrap(Application $app)
53
    {
54
        $this->detectEnvironmentFile($app);
55
        try
56
        {
57
            $this->createEnv($app)->load();
58
        }
59
        catch (Exception $e)
60
        {
61
            //
62
        }
63
    }
64
65
    /**
66
     * Detect if a custom environment file matching the APP_ENV exists.
67
     * 
68
     * @param  \Syscodes\Contracts\Core\Application  $app
69
     * 
70
     * @return bool
71
     */
72
    protected function detectEnvironmentFile($app)
73
    {
74
        $environment = env('APP_ENV');
75
76
        if ( ! $environment) {
77
            return;
78
        }
79
80
        $this->setEnvironmentFilePath(
81
            $app, $app->environmentFile().'.'.$environment
0 ignored issues
show
Bug introduced by
Are you sure $environment of type mixed|string|true can be used in concatenation? ( Ignorable by Annotation )

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

81
            $app, $app->environmentFile().'.'./** @scrutinizer ignore-type */ $environment
Loading history...
82
        );
83
    }
84
85
    /**
86
     * Load a custom environment file.
87
     * 
88
     * @param  \Syscodes\Contracts\Core\Application  $app
89
     * @param  string  $file
90
     * 
91
     * @return bool
92
     */
93
    protected function setEnvironmentFilePath($app, $file)
94
    {
95
        if (is_file($app->environmentPath().'/'.$file)) {
0 ignored issues
show
Bug introduced by
The method environmentPath() does not exist on Syscodes\Contracts\Core\Application. Did you maybe mean environmentFilePath()? ( Ignorable by Annotation )

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

95
        if (is_file($app->/** @scrutinizer ignore-call */ environmentPath().'/'.$file)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
            $app->setEnvironmentFile($file);
0 ignored issues
show
Bug introduced by
The method setEnvironmentFile() does not exist on Syscodes\Contracts\Core\Application. Since it exists in all sub-types, consider adding an abstract or default implementation to Syscodes\Contracts\Core\Application. ( Ignorable by Annotation )

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

96
            $app->/** @scrutinizer ignore-call */ 
97
                  setEnvironmentFile($file);
Loading history...
97
98
            return true;
99
        }
100
101
        return false;
102
    }
103
104
    /**
105
     * Create a ParseEnv instance.
106
     * 
107
     * @param  \Syscodes\Contracts\Core\Application  $app
108
     * 
109
     * @return \Syscodes\Dotenv\Dotenv
110
     */
111
    protected function createEnv($app)
112
    {
113
        return Dotenv::create(
114
               new RepositoryCreator,
115
               $app->environmentPath(),
116
               $app->environmentFile()
117
        );
118
    }
119
}