Passed
Push — 0.7.0 ( a316e5...a89e02 )
by Alexander
03:01
created

DefineAdapter::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
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.
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
21
 */
22
23
namespace Syscodes\Dotenv\Repository\Adapters;
24
25
use Syscodes\Contracts\Dotenv\Adapter;
26
27
/**
28
 * Read, write and delete an environment variable for 
29
 * process of define.
30
 * 
31
 * @author Alexander Campo <[email protected]>
32
 */
33
class DefineAdapter implements Adapter
34
{
35
    /**
36
     * Determines if the adapter is supported.
37
     * 
38
     * @return bool
39
     */
40
    public function isSupported()
41
    {
42
        return true;
43
    }
44
45
    /**
46
     * Check if a variable exists.
47
     * 
48
     * @param  string  $name
49
     * 
50
     * @return bool
51
     */
52
    public function has(string $name)
53
    {
54
        return defined($name);
55
    }
56
57
    /**
58
     * Read an environment variable.
59
     * 
60
     * @param  string  $name
61
     * 
62
     * @return mixed
63
     */
64
    public function read(string $name)
65
    {
66
        if ($this->has($name)) {
67
            return constant($name);
68
        }
69
70
        return null;
71
    }
72
73
     /**
74
     * Write to an environment variable.
75
     * 
76
     * @param  string  $name
77
     * @param  string  $value
78
     * 
79
     * @return bool
80
     */
81
    public function write(string $name, string $value)
82
    {
83
        if ( ! $this->has($name)) {
84
            return define($name, $value);
85
        }
86
87
        return true;
88
    }
89
90
    /**
91
     * Delete an environment variable.
92
     * 
93
     * @param  string  $name
94
     * 
95
     * @return bool
96
     */
97
    public function delete(string $name)
98
    {
99
        if ($this->has($name)) {
100
            return define($name, '');
101
        }
102
103
        return true;
104
    }
105
}