Completed
Push — master ( 5bfdc5...fda08e )
by Sebastien
03:04
created

Project::addAlsoAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * Drush Cerbere command line tools.
5
 * Copyright (C) 2015 - Sebastien Malot <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 */
21
22
namespace Cerbere\Model;
23
24
/**
25
 * Class Project
26
 *
27
 * @package Cerbere\Model
28
 */
29
class Project
30
{
31
    /**
32
     * URL to check for updates, if a given project doesn't define its own.
33
     */
34
    const UPDATE_DEFAULT_URL = 'http://updates.drupal.org/release-history';
35
36
    /**
37
     *
38
     */
39
    const INSTALL_TYPE_OFFICIAL = 'official';
40
41
    /**
42
     *
43
     */
44
    const INSTALL_TYPE_DEV = 'dev';
45
46
    /**
47
     *
48
     */
49
    const INSTALL_TYPE_UNKNOWN = 'unknown';
50
51
    /**
52
     *
53
     */
54
    const TYPE_PROJECT_DISTRIBUTION = 'project_distribution';
55
56
    /**
57
     *
58
     */
59
    const TYPE_PROJECT_CORE = 'project_core';
60
61
    /**
62
     *
63
     */
64
    const TYPE_PROJECT_MODULE = 'project_module';
65
66
    /**
67
     *
68
     */
69
    const TYPE_PROJECT_THEME = 'project_theme';
70
71
    /**
72
     *
73
     */
74
    const TYPE_UNKNOWN = 'unknown';
75
76
    /**
77
     * @var
78
     */
79
    protected $project;
80
81
    /**
82
     * @var string
83
     */
84
    protected $filename;
85
86
    /**
87
     * @var
88
     */
89
    protected $name;
90
91
    /**
92
     * @var
93
     */
94
    protected $core;
95
96
    /**
97
     * @var
98
     */
99
    protected $version;
100
101
    /**
102
     * @var
103
     */
104
    protected $status_url;
105
106
    /**
107
     * @var
108
     */
109
    protected $install_type;
110
111
    /**
112
     * @var
113
     */
114
    protected $existing_version;
115
116
    /**
117
     * @var
118
     */
119
    protected $existing_major;
120
121
    /**
122
     * @var array
123
     */
124
    protected $data;
125
126
    // Calculated properties.
127
128
    /**
129
     * @var
130
     */
131
    protected $status;
132
133
    /**
134
     * @var
135
     */
136
    protected $project_status;
137
138
    /**
139
     * @var string
140
     */
141
    protected $project_type;
142
143
    /**
144
     * @var
145
     */
146
    protected $reason;
147
148
    /**
149
     * @var
150
     */
151
    protected $fetch_status;
152
153
    /**
154
     * @var
155
     */
156
    protected $latest_version;
157
158
    /**
159
     * @var
160
     */
161
    protected $latest_dev;
162
163
    /**
164
     * @var
165
     */
166
    protected $dev_version;
167
168
    /**
169
     * @var
170
     */
171
    protected $recommended;
172
173
    /**
174
     * @var
175
     */
176
    protected $datestamp;
177
178
    /**
179
     * @var array
180
     */
181
    protected $releases;
182
183
    /**
184
     * @var array
185
     */
186
    protected $security_updates;
187
188
    /**
189
     * @var array
190
     */
191
    protected $also_available;
192
193
    /**
194
     * @param string $project
195
     * @param string $core
196
     * @param string $version
197
     */
198
    public function __construct($project, $core, $version)
199
    {
200
        $this->project = $project;
201
        $this->name    = $project;
202
        $this->core    = $core;
203
        $this->version = $version;
204
205
        $this->project_type = self::TYPE_UNKNOWN;
206
207
        $this->releases         = array();
208
        $this->security_updates = array();
209
        $this->also_available   = array();
210
211
        $this->init();
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    public function getFilename()
218
    {
219
        return $this->filename;
220
    }
221
222
    /**
223
     * @param string $filename
224
     */
225
    public function setFilename($filename)
226
    {
227
        $this->filename = $filename;
228
229
        $this->init();
230
    }
231
232
    /**
233
     *
234
     */
235
    protected function init()
236
    {
237
        // Patch project if Drupal detected.
238
        if (isset($this->data['package']) && strtolower($this->data['package']) == 'core') {
239
            $this->name = 'Drupal';
240
241
            $this->data = array(
242
              'name' => $this->name,
243
              'description' => '',
244
              'package' => $this->data['package'],
245
              'core' => $this->data['core'],
246
              'version' => $this->data['version'],
247
              'files' => array(),
248
              'configure' => '',
249
              'project' => 'drupal',
250
              'datestamp' => $this->data['datestamp'],
251
            );
252
        }
253
254
        $this->status_url = self::UPDATE_DEFAULT_URL;
255
256
        // Assume an official release until we see otherwise.
257
        $this->install_type     = self::INSTALL_TYPE_OFFICIAL;
258
        $this->existing_version = $this->version;
259
260
        if (isset($this->version)) {
261
            // Check for development snapshots
262
            if (preg_match('/(dev|HEAD)/', $this->version)) {
263
                $this->install_type = self::INSTALL_TYPE_DEV;
264
            }
265
266
            // Figure out what the currently installed major version is. We need
267
            // to handle both contribution (e.g. "5.x-1.3", major = 1) and core
268
            // (e.g. "5.1", major = 5) version strings.
269
            $matches = array();
270
            if (preg_match('/^(\d+\.x-)?(\d+)\..*$/', $this->version, $matches)) {
271
                $this->existing_major = $matches[2];
272
            } else {
273
                // This would only happen for version strings that don't follow the
274
                // drupal.org convention. We let contribs define "major" in their
275
                // .info in this case, and only if that's missing would we hit this.
276
                $this->existing_major = -1;
277
            }
278
        } else {
279
            // No version info available at all.
280
            $this->install_type     = self::INSTALL_TYPE_UNKNOWN;
281
            $this->existing_version = 'Unknown';
282
            $this->existing_major   = -1;
283
        }
284
    }
285
286
    /**
287
     * @param string  $version
288
     * @param Release $release
289
     */
290
    public function addSecurityUpdate($version, $release)
291
    {
292
        $this->security_updates[$version] = $release;
293
    }
294
295
    /**
296
     * @param string  $version
297
     * @param Release $release
298
     */
299
    public function addAlsoAvailable($version, $release)
300
    {
301
        $this->also_available[$version] = $release;
302
    }
303
304
    /**
305
     * @param string  $version
306
     * @return mixed
307
     */
308
    public function getAlsoAvailable($version)
309
    {
310
        return $this->also_available[$version];
311
    }
312
313
    /**
314
     * @return mixed
315
     */
316
    public function getCore()
317
    {
318
        return $this->core;
319
    }
320
321
    /**
322
     * @return mixed
323
     */
324
    public function getDatestamp()
325
    {
326
        return $this->datestamp;
327
    }
328
329
    /**
330
     * @return array
331
     */
332
    public function getDetails()
333
    {
334
        return $this->data;
335
    }
336
337
    /**
338
     * @return string
339
     */
340
    public function getProjectType()
341
    {
342
        return $this->project_type;
343
    }
344
345
    /**
346
     * @param string $type
347
     */
348
    public function setProjectType($type)
349
    {
350
        $this->project_type = $type;
351
    }
352
353
    /**
354
     * @return mixed
355
     */
356
    public function getDevVersion()
357
    {
358
        return $this->dev_version;
359
    }
360
361
    /**
362
     * @param $dev_version
363
     */
364
    public function setDevVersion($dev_version)
365
    {
366
        $this->dev_version = $dev_version;
367
    }
368
369
    /**
370
     * @return string
371
     */
372
    public function getExistingMajor()
373
    {
374
        return $this->existing_major;
375
    }
376
377
    /**
378
     * @return string
379
     */
380
    public function getExistingVersion()
381
    {
382
        return $this->existing_version;
383
    }
384
385
    /**
386
     * @return integer
387
     */
388
    public function getFetchStatus()
389
    {
390
        return $this->fetch_status;
391
    }
392
393
    /**
394
     * @param $fetch_status
395
     */
396
    public function setFetchStatus($fetch_status)
397
    {
398
        $this->fetch_status = $fetch_status;
399
    }
400
401
    /**
402
     * @return string
403
     */
404
    public function getInstallType()
405
    {
406
        return $this->install_type;
407
    }
408
409
    /**
410
     * @return mixed
411
     */
412
    public function getLatestDev()
413
    {
414
        return $this->latest_dev;
415
    }
416
417
    /**
418
     * @param $latest_dev
419
     */
420
    public function setLatestDev($latest_dev)
421
    {
422
        $this->latest_dev = $latest_dev;
423
    }
424
425
    /**
426
     * @return mixed
427
     */
428
    public function getLatestVersion()
429
    {
430
        return $this->latest_version;
431
    }
432
433
    /**
434
     * @param $latest_version
435
     */
436
    public function setLatestVersion($latest_version)
437
    {
438
        $this->latest_version = $latest_version;
439
    }
440
441
    /**
442
     * @return string
443
     */
444
    public function getName()
445
    {
446
        return $this->name;
447
    }
448
449
    /**
450
     * @return string
451
     */
452
    public function getProject()
453
    {
454
        return $this->project;
455
    }
456
457
    /**
458
     * @return int
459
     */
460
    public function getProjectStatus()
461
    {
462
        return $this->project_status;
463
    }
464
465
    /**
466
     * @param int $project_status
467
     */
468
    public function setProjectStatus($project_status)
469
    {
470
        $this->project_status = $project_status;
471
    }
472
473
    /**
474
     * @return string
475
     */
476
    public function getReason()
477
    {
478
        return $this->reason;
479
    }
480
481
    /**
482
     * @param string $reason
483
     */
484
    public function setReason($reason)
485
    {
486
        $this->reason = $reason;
487
    }
488
489
    /**
490
     * @return mixed
491
     */
492
    public function getRecommended()
493
    {
494
        return $this->recommended;
495
    }
496
497
    /**
498
     * @param $recommended
499
     */
500
    public function setRecommended($recommended)
501
    {
502
        $this->recommended = $recommended;
503
    }
504
505
    /**
506
     * @return Release[]
507
     */
508
    public function getReleases()
509
    {
510
        return $this->releases;
511
    }
512
513
    /**
514
     * @param Release[] $releases
515
     */
516
    public function setReleases($releases)
517
    {
518
        $this->releases = $releases;
519
    }
520
521
    /**
522
     * @param string $release
523
     *
524
     * @return Release|false
525
     */
526
    public function getRelease($release)
527
    {
528
        if (isset($this->releases[$release])) {
529
            return $this->releases[$release];
530
        }
531
532
        return false;
533
    }
534
535
    /**
536
     * @return Release[]
537
     */
538
    public function getSecurityUpdates()
539
    {
540
        return $this->security_updates;
541
    }
542
543
    /**
544
     * @param Release[] $security_updates
545
     */
546
    public function setSecurityUpdates($security_updates)
547
    {
548
        $this->security_updates = $security_updates;
549
    }
550
551
    /**
552
     * @return int
553
     */
554
    public function getStatus()
555
    {
556
        return $this->status;
557
    }
558
559
    /**
560
     * @param int $status
561
     */
562
    public function setStatus($status)
563
    {
564
        $this->status = $status;
565
    }
566
567
    /**
568
     * @return string
569
     */
570
    public function getStatusUrl()
571
    {
572
        return $this->status_url;
573
    }
574
575
    /**
576
     * @param string $status_url
577
     */
578
    public function setStatusUrl($status_url)
579
    {
580
        $this->status_url = $status_url;
581
    }
582
583
    /**
584
     * @return string
585
     */
586
    public function getVersion()
587
    {
588
        return $this->version;
589
    }
590
591
    /**
592
     * @return bool
593
     */
594
    public function hasSecurityUpdates()
595
    {
596
        return count($this->security_updates) > 0;
597
    }
598
599
    /**
600
     * @param array $data
601
     */
602
    public function setDetails($data)
603
    {
604
        $this->data = $data;
605
606
        foreach (array('name', 'core', 'version', 'datestamp') as $property) {
607
            if (isset($data[$property])) {
608
                $this->$property = $data[$property];
609
            }
610
        }
611
612
        $this->init();
613
    }
614
615
    /**
616
     * @param string  $version
617
     * @param Release $release
618
     */
619
    public function setRelease($version, Release $release)
620
    {
621
        $this->releases[$version] = $release;
622
    }
623
}
624