Completed
Push — master ( fda08e...d5e2fd )
by Sebastien
02:30
created

Project::hasAlsoAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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_major
297
     * @param string $version
298
     */
299
    public function addAlsoAvailable($version_major, $version)
300
    {
301
        $this->also_available[$version_major] = $version;
302
    }
303
304
    /**
305
     * @return array
306
     */
307
    public function getAlsoAvailable()
308
    {
309
        return $this->also_available;
310
    }
311
312
    /**
313
     * @param string $version_major
314
     * @return bool
315
     */
316
    public function hasAlsoAvailable($version_major)
317
    {
318
        return isset($this->also_available[$version_major]);
319
    }
320
321
    /**
322
     * @return mixed
323
     */
324
    public function getCore()
325
    {
326
        return $this->core;
327
    }
328
329
    /**
330
     * @return mixed
331
     */
332
    public function getDatestamp()
333
    {
334
        return $this->datestamp;
335
    }
336
337
    /**
338
     * @return array
339
     */
340
    public function getDetails()
341
    {
342
        return $this->data;
343
    }
344
345
    /**
346
     * @return string
347
     */
348
    public function getProjectType()
349
    {
350
        return $this->project_type;
351
    }
352
353
    /**
354
     * @param string $type
355
     */
356
    public function setProjectType($type)
357
    {
358
        $this->project_type = $type;
359
    }
360
361
    /**
362
     * @return mixed
363
     */
364
    public function getDevVersion()
365
    {
366
        return $this->dev_version;
367
    }
368
369
    /**
370
     * @param $dev_version
371
     */
372
    public function setDevVersion($dev_version)
373
    {
374
        $this->dev_version = $dev_version;
375
    }
376
377
    /**
378
     * @return string
379
     */
380
    public function getExistingMajor()
381
    {
382
        return $this->existing_major;
383
    }
384
385
    /**
386
     * @return string
387
     */
388
    public function getExistingVersion()
389
    {
390
        return $this->existing_version;
391
    }
392
393
    /**
394
     * @return integer
395
     */
396
    public function getFetchStatus()
397
    {
398
        return $this->fetch_status;
399
    }
400
401
    /**
402
     * @param $fetch_status
403
     */
404
    public function setFetchStatus($fetch_status)
405
    {
406
        $this->fetch_status = $fetch_status;
407
    }
408
409
    /**
410
     * @return string
411
     */
412
    public function getInstallType()
413
    {
414
        return $this->install_type;
415
    }
416
417
    /**
418
     * @return mixed
419
     */
420
    public function getLatestDev()
421
    {
422
        return $this->latest_dev;
423
    }
424
425
    /**
426
     * @param $latest_dev
427
     */
428
    public function setLatestDev($latest_dev)
429
    {
430
        $this->latest_dev = $latest_dev;
431
    }
432
433
    /**
434
     * @return mixed
435
     */
436
    public function getLatestVersion()
437
    {
438
        return $this->latest_version;
439
    }
440
441
    /**
442
     * @param $latest_version
443
     */
444
    public function setLatestVersion($latest_version)
445
    {
446
        $this->latest_version = $latest_version;
447
    }
448
449
    /**
450
     * @return string
451
     */
452
    public function getName()
453
    {
454
        return $this->name;
455
    }
456
457
    /**
458
     * @return string
459
     */
460
    public function getProject()
461
    {
462
        return $this->project;
463
    }
464
465
    /**
466
     * @return int
467
     */
468
    public function getProjectStatus()
469
    {
470
        return $this->project_status;
471
    }
472
473
    /**
474
     * @param int $project_status
475
     */
476
    public function setProjectStatus($project_status)
477
    {
478
        $this->project_status = $project_status;
479
    }
480
481
    /**
482
     * @return string
483
     */
484
    public function getReason()
485
    {
486
        return $this->reason;
487
    }
488
489
    /**
490
     * @param string $reason
491
     */
492
    public function setReason($reason)
493
    {
494
        $this->reason = $reason;
495
    }
496
497
    /**
498
     * @return mixed
499
     */
500
    public function getRecommended()
501
    {
502
        return $this->recommended;
503
    }
504
505
    /**
506
     * @param $recommended
507
     */
508
    public function setRecommended($recommended)
509
    {
510
        $this->recommended = $recommended;
511
    }
512
513
    /**
514
     * @return Release[]
515
     */
516
    public function getReleases()
517
    {
518
        return $this->releases;
519
    }
520
521
    /**
522
     * @param Release[] $releases
523
     */
524
    public function setReleases($releases)
525
    {
526
        $this->releases = $releases;
527
    }
528
529
    /**
530
     * @param string $release
531
     *
532
     * @return Release|false
533
     */
534
    public function getRelease($release)
535
    {
536
        if (isset($this->releases[$release])) {
537
            return $this->releases[$release];
538
        }
539
540
        return false;
541
    }
542
543
    /**
544
     * @return Release[]
545
     */
546
    public function getSecurityUpdates()
547
    {
548
        return $this->security_updates;
549
    }
550
551
    /**
552
     * @param Release[] $security_updates
553
     */
554
    public function setSecurityUpdates($security_updates)
555
    {
556
        $this->security_updates = $security_updates;
557
    }
558
559
    /**
560
     * @return int
561
     */
562
    public function getStatus()
563
    {
564
        return $this->status;
565
    }
566
567
    /**
568
     * @param int $status
569
     */
570
    public function setStatus($status)
571
    {
572
        $this->status = $status;
573
    }
574
575
    /**
576
     * @return string
577
     */
578
    public function getStatusUrl()
579
    {
580
        return $this->status_url;
581
    }
582
583
    /**
584
     * @param string $status_url
585
     */
586
    public function setStatusUrl($status_url)
587
    {
588
        $this->status_url = $status_url;
589
    }
590
591
    /**
592
     * @return string
593
     */
594
    public function getVersion()
595
    {
596
        return $this->version;
597
    }
598
599
    /**
600
     * @return bool
601
     */
602
    public function hasSecurityUpdates()
603
    {
604
        return count($this->security_updates) > 0;
605
    }
606
607
    /**
608
     * @param array $data
609
     */
610
    public function setDetails($data)
611
    {
612
        $this->data = $data;
613
614
        foreach (array('name', 'core', 'version', 'datestamp') as $property) {
615
            if (isset($data[$property])) {
616
                $this->$property = $data[$property];
617
            }
618
        }
619
620
        $this->init();
621
    }
622
623
    /**
624
     * @param string  $version
625
     * @param Release $release
626
     */
627
    public function setRelease($version, Release $release)
628
    {
629
        $this->releases[$version] = $release;
630
    }
631
}
632