• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

violinist-dev / project-data / 10134433356

28 Jul 2024 08:09PM UTC coverage: 79.545% (+0.06%) from 79.487%
10134433356

push

github

web-flow
Use dotenv for parsing env string, and add method for getting as array (#1)

3 of 5 new or added lines in 1 file covered. (60.0%)

35 of 44 relevant lines covered (79.55%)

4.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.55
/src/ProjectData.php
1
<?php
2

3
namespace Violinist\ProjectData;
4

5
use Symfony\Component\Dotenv\Dotenv;
6

7
class ProjectData
8
{
9

10
    /**
11
     * The nid.
12
     *
13
     * @var int
14
     */
15
    protected $nid;
16

17
    /**
18
     * The php version.
19
     *
20
     * @var string
21
     */
22
    protected $phpVersion;
23

24
    /**
25
     * @var string
26
     */
27
    protected $customPrMessage;
28

29
    /**
30
     * Roles for the project, inherited from the user.
31
     *
32
     * @var array
33
     */
34
    protected $roles;
35

36
    /**
37
     * Should we update all?
38
     *
39
     * @var bool
40
     */
41
    protected $updateAll;
42

43
    /**
44
     * Either a path to a directory (without the last slash) or null (means root).
45
     *
46
     * @var string|null
47
     */
48
    protected $composerJsonDir;
49

50
    /**
51
     * ENV string.
52
     *
53
     * @var string
54
     */
55
    protected $envString;
56

57
    /**
58
     * @return string
59
     */
60
    public function getEnvString()
61
    {
62
        return $this->envString;
7✔
63
    }
64

65
    public function getEnvArray() : array
66
    {
67
        $env = $this->getEnvString();
1✔
68
        $dotenv_data = new Dotenv();
1✔
69
        try {
70
            return $dotenv_data->parse($env);
1✔
NEW
71
        } catch (\Throwable $e) {
×
NEW
72
            return [];
×
73
        }
74
    }
75

76
    /**
77
     * @param string $envString
78
     */
79
    public function setEnvString($envString)
80
    {
81
        $this->envString = $envString;
7✔
82
    }
83

84
    /**
85
     * @return string|null
86
     */
87
    public function getComposerJsonDir()
88
    {
89
        return $this->composerJsonDir;
×
90
    }
91

92
    /**
93
     * @param string|null $composerJsonDir
94
     */
95
    public function setComposerJsonDir($composerJsonDir)
96
    {
97
        $this->composerJsonDir = $composerJsonDir;
×
98
    }
99

100
    /**
101
     * @return array
102
     */
103
    public function getRoles()
104
    {
105
        return $this->roles;
1✔
106
    }
107

108
    /**
109
     * @param array $roles
110
     */
111
    public function setRoles($roles)
112
    {
113
        $this->roles = $roles;
7✔
114
    }
115

116
    /**
117
     * @return string
118
     */
119
    public function getCustomPrMessage()
120
    {
121
        return $this->customPrMessage;
8✔
122
    }
123

124
    /**
125
     * @param string $customPrMessage
126
     */
127
    public function setCustomPrMessage($customPrMessage)
128
    {
129
        $this->customPrMessage = $customPrMessage;
4✔
130
    }
131

132
    /**
133
     * Get the nid.
134
     *
135
     * @return int
136
     *   Nid.
137
     */
138
    public function getNid()
139
    {
140
        return $this->nid;
6✔
141
    }
142

143
    public function shouldUpdateAll()
144
    {
145
        return $this->updateAll;
×
146
    }
147

148
    /**
149
     * Set the nid.
150
     *
151
     * @param int $nid
152
     *   A nid.
153
     */
154
    public function setNid($nid)
155
    {
156
        $this->nid = $nid;
7✔
157
    }
158

159
    /**
160
     * Get the php version.
161
     *
162
     * @return string
163
     *   The php version.
164
     */
165
    public function getPhpVersion()
166
    {
167
        return $this->phpVersion;
5✔
168
    }
169

170
    /**
171
     * Set the php version.
172
     *
173
     * @param string $phpVersion
174
     *   The php version.
175
     */
176
    public function setPhpVersion($phpVersion)
177
    {
178
        $this->phpVersion = $phpVersion;
7✔
179
    }
180

181
    /**
182
     * Set the update all flag.
183
     */
184
    public function setUpdateAll($update)
185
    {
186
        $this->updateAll = (bool) $update;
×
187
    }
188

189
    /**
190
     * Create an object from a node.
191
     *
192
     * @param \Drupal\node\NodeInterface $node
193
     *   A node.
194
     *
195
     * @return \Violinist\ProjectData\ProjectData
196
     *   A new project.
197
     */
198
    public static function fromNode($node)
199
    {
200
        $p = new self();
7✔
201
        $p->setNid($node->id());
7✔
202
        $p->setPhpVersion('7.0');
7✔
203
        if ($node->hasField('field_pull_request_template') && !$node->get('field_pull_request_template')->isEmpty()) {
7✔
204
            $p->setCustomPrMessage($node->get('field_pull_request_template')->first()->getString());
2✔
205
        }
206
        if (!$p->getCustomPrMessage()) {
7✔
207
            // See if we have a default one on the user.
208
            $owner = $node->getOwner();
5✔
209
            if ($owner->hasField('field_user_pr_template') && !$owner->get('field_user_pr_template')->isEmpty()) {
5✔
210
                $p->setCustomPrMessage($owner->get('field_user_pr_template')->first()->getString());
1✔
211
            }
212
        }
213
        if ($node->hasField('field_php_version') && !$node->get('field_php_version')->isEmpty()) {
7✔
214
            $p->setPhpVersion($node->get('field_php_version')->first()->getString());
1✔
215
        }
216
        if ($node->hasField('field_composer_json_path') && !$node->get('field_composer_json_path')->isEmpty()) {
7✔
217
            $directory = $node->get('field_composer_json_path')->first()->getString();
×
218
            $p->setComposerJsonDir($directory);
×
219
        }
220
        $owner = $node->getOwner();
7✔
221
        if ($owner) {
7✔
222
            $p->setRoles($owner->getRoles());
7✔
223
        }
224
        $p->setEnvString('');
7✔
225
        if ($node->hasField('field_environment_variables') && !$node->get('field_environment_variables')->isEmpty()) {
7✔
226
            $p->setEnvString($node->get('field_environment_variables')->first()->getString());
1✔
227
        }
228
        if (!$p->getEnvString()) {
7✔
229
            // Use the user one. If available.
230
            if ($owner) {
6✔
231
                if ($owner->hasField('field_environment_variables') && !$owner->get('field_environment_variables')->isEmpty()) {
6✔
232
                    $p->setEnvString($owner->get('field_environment_variables')->first()->getString());
×
233
                }
234
            }
235
        }
236
        return $p;
7✔
237
    }
238
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc