Completed
Push — master ( 9ab068...67c411 )
by Pierre
04:25
created

Package::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Ptondereau\PackMe;
4
5
use Illuminate\Support\Str;
6
use Ptondereau\PackMe\Validators\Validator;
7
8
/**
9
 * Class Package.
10
 */
11
class Package
12
{
13
    /**
14
     * Full name of package. (e.g: vendor/package).
15
     *
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * Package name (e.g: YourPackage).
22
     *
23
     * @var string
24
     */
25
    protected $package;
26
27
    /**
28
     * Vendor name (e.g: YourVendor).
29
     *
30
     * @var string
31
     */
32
    protected $vendor;
33
34
    /**
35
     * Full author string. (e.g: John Smith <[email protected]>).
36
     *
37
     * @var string
38
     */
39
    protected $author;
40
41
    /**
42
     * Author's email.
43
     *
44
     * @var string
45
     */
46
    protected $email;
47
48
    /**
49
     * Author's full name.
50
     *
51
     * @var string
52
     */
53
    protected $authorName;
54
55
    /**
56
     * Package's description.
57
     *
58
     * @var string
59
     */
60
    protected $description = '';
61
62
    /**
63
     * Path destination for all generated files.
64
     *
65
     * @var string
66
     */
67
    protected $destination;
68
69
    /**
70
     * Validator for the package.
71
     *
72
     * @var Validator
73
     */
74
    protected $validator;
75
76
    /**
77
     * Package constructor.
78
     *
79
     * @param Validator   $validator
80
     * @param null|string $name
81
     * @param null|string $author
82
     * @param null|string $destination
83
     */
84 21
    public function __construct($name = null, $author = null, $destination = null, Validator $validator = null)
85
    {
86 21
        $this->validator = $validator ?: new Validator();
87 21
        $this->name = $name;
88 21
        $this->author = $author;
89 21
        $this->destination = $destination;
90 21
    }
91
92
    /**
93
     * Return keywords list.
94
     *
95
     * @return array
96
     */
97 6
    public function toArray()
98
    {
99 6
        $this->validator->verify($this);
100
101 6
        $this->parseAuthor($this->author);
102
103 6
        $packageInfo = explode('/', $this->name);
104
105 6
        $this->vendor = Str::studly($packageInfo[0]);
106 6
        $this->package = Str::studly($packageInfo[1]);
107
108
        return [
109 6
            'name'        => $this->name,
110 6
            'description' => $this->description ?: '',
111 6
            'vendor'      => $this->vendor,
112 6
            'package'     => $this->package,
113 6
            'authorName'  => $this->authorName,
114 6
            'authorEmail' => $this->email,
115 6
            'config'      => Str::slug($this->package),
116 6
        ];
117
    }
118
119
    /**
120
     * @return string
121
     */
122 12
    public function getName()
123
    {
124 12
        return $this->name;
125
    }
126
127
    /**
128
     * @param string $name
129
     */
130 6
    public function setName($name)
131
    {
132 6
        $this->name = $name;
133 6
    }
134
135
    /**
136
     * @return string
137
     */
138 3
    public function getPackage()
139
    {
140 3
        return $this->package;
141
    }
142
143
    /**
144
     * @param string $package
145
     */
146 3
    public function setPackage($package)
147
    {
148 3
        $this->package = $package;
149 3
    }
150
151
    /**
152
     * @return string
153
     */
154 3
    public function getVendor()
155
    {
156 3
        return $this->vendor;
157
    }
158
159
    /**
160
     * @param string $vendor
161
     */
162 3
    public function setVendor($vendor)
163
    {
164 3
        $this->vendor = $vendor;
165 3
    }
166
167
    /**
168
     * @return string
169
     */
170 9
    public function getAuthor()
171
    {
172 9
        return $this->author;
173
    }
174
175
    /**
176
     * @param string $author
177
     */
178 3
    public function setAuthor($author)
179
    {
180 3
        $this->author = $author;
181 3
    }
182
183
    /**
184
     * @return string
185
     */
186 3
    public function getEmail()
187
    {
188 3
        if (!$this->email && $this->author) {
189 3
            $this->parseAuthor($this->author);
190 3
        }
191
192 3
        return $this->email;
193
    }
194
195
    /**
196
     * @param string $email
197
     */
198 3
    public function setEmail($email)
199
    {
200 3
        $this->email = $email;
201 3
    }
202
203
    /**
204
     * @return string
205
     */
206 3
    public function getAuthorName()
207
    {
208 3
        if (!$this->authorName && $this->author) {
209 3
            $this->parseAuthor($this->author);
210 3
        }
211
212 3
        return $this->authorName;
213
    }
214
215
    /**
216
     * @param string $authorName
217
     */
218 3
    public function setAuthorName($authorName)
219
    {
220 3
        $this->authorName = $authorName;
221 3
    }
222
223
    /**
224
     * @return string
225
     */
226 3
    public function getDescription()
227
    {
228 3
        return $this->description;
229
    }
230
231
    /**
232
     * @param string $description
233
     */
234 3
    public function setDescription($description)
235
    {
236 3
        $this->description = $description;
237 3
    }
238
239
    /**
240
     * @return string
241
     */
242 18
    public function getDestination()
243
    {
244 18
        return getcwd().'/'.$this->destination;
245
    }
246
247
    /**
248
     * @param string $destination
249
     */
250 6
    public function setDestination($destination)
251
    {
252 6
        $this->destination = $destination;
253 6
    }
254
255
    /**
256
     * @param $string
257
     */
258 9
    protected function parseAuthor($string)
259
    {
260 9
        if (preg_match('/^(?P<name>[- \.,\p{L}\p{N}\'’]+) <(?P<email>.+?)>$/u', $string, $match)) {
261 9
            $this->email = $match['email'];
262 9
            $this->authorName = trim($match['name']);
263 9
        }
264 9
    }
265
}
266