Completed
Push — master ( 5955f6...652d4a )
by Andrii
03:36
created

PackageController::getLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * Automation tool mixed with code generator for easier continuous development
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
use hidev\helpers\Helper;
15
16
/**
17
 * Package part of the config.
18
 *
19
 * @property string name
20
 * @property string year
21
 * @property string source
22
 */
23
class PackageController extends CommonController
24
{
25
    use \hiqdev\yii2\collection\ObjectTrait;
26
27
    public function getType()
28
    {
29
        return $this->getItem('type') ?: 'project';
30
    }
31
32
    public function getLanguage()
33
    {
34
        return $this->getItem('language') ?: 'php';
35
    }
36
37 2
    public function getYears()
38
    {
39
        $years = $this->getItem('years');
40 2
        if (!empty($years)) {
41 2
            return $years;
42
        }
43
        $cur = (int) date('Y');
44
        $old = (int) $this->year;
45
46
        return ($old && $old < $cur ? $this->year . '-' : '') . $cur;
47
    }
48
49
    public function getYear()
50
    {
51
        return $this->getItem('year') ?: $this->takeVcs()->getYear();
52
    }
53
54
    public function getLicense()
55
    {
56
        return $this->getItem('license') ?: $this->takeVendor()->license ?: 'No license';
57
    }
58
59
    public function getIssues()
60
    {
61
        return $this->getItem('issues') ?: ($this->source . '/issues');
62
    }
63
64
    public function getWiki()
65
    {
66
        return $this->getItem('wiki') ?: ($this->source . '/wiki');
67
    }
68
69
    public function getKeywords()
70
    {
71
        return Helper::csplit($this->getItem('keywords'));
72
    }
73
74
    public function getFullName()
75
    {
76
        return $this->getItem('fullName') ?: ($this->takeVendor()->name . '/' . $this->name);
77
    }
78
79
    public function getSource()
80
    {
81
        return $this->getItem('source') ?: ('https://github.com/' . $this->takeGoal('github')->full_name);
82
    }
83
84
    public function getVersion()
85
    {
86
        return $this->takeGoal('version')->version;
87
    }
88
89
    public function getNamespace()
90
    {
91
        return $this->getItem('namespace') ?: $this->getPackageManager()->namespace ?: self::defaultNamespace($this->takeVendor()->name, $this->name);
92
    }
93
94
    public static function defaultNamespace($vendor, $package)
95
    {
96
        return preg_replace('/[^a-zA-Z0-9\\\\]+/', '', $vendor . strtr("-$package", '-', '\\'));
97
    }
98
99
    public function getSrc()
100
    {
101
        $src = $this->rawItem('src');
102
103
        return isset($src) ? $src : 'src';
104
    }
105
106
    public function getHomepage()
107
    {
108
        return $this->getItem('homepage') ?: ($this->isDomain() ? 'http://' . $this->name . '/' : $this->getSource());
109
    }
110
111
    public function getForum()
112
    {
113
        return $this->getItem('forum') ?: $this->takeVendor()->forum;
114
    }
115
116
    public function getLabel()
117
    {
118
        return $this->getItem('label') ?: $this->getHeadline() ?: Helper::id2camel($this->name);
119
    }
120
121
    public function isDomain()
122
    {
123
        return preg_match('/^[a-z0-9-]+(\.[a-z0-9]+)+$/', $this->name);
124
    }
125
126
    public function getTitle()
127
    {
128
        return $this->getItem('title') ?: ($this->isDomain() ? $this->name : Helper::titleize($this->name));
129
    }
130
131
    public function getHeadline()
132
    {
133
        return $this->getItem('headline');
134
    }
135
136
    public function getDescription()
137
    {
138
        return $this->getItem('description');
139
    }
140
141
    public function getRepositoryUrl($file)
142
    {
143
        return 'https://github.com/' . $this->getFullName() . '/blob/master/' . $file;
144
    }
145
    public function getAuthors()
146
    {
147
        return $this->getItem('authors') ?: $this->takeVendor()->authors;
148
    }
149
150
    /**
151
     * Composer for the moment.
152
     * To be changed to get actual Package Manager.
153
     */
154
    public function getPackageManager()
155
    {
156
        return $this->takeGoal('composer');
157
    }
158
159
    public function hasRequireAny($package)
160
    {
161
        return $this->hasRequire($package) || $this->hasRequireDev($package);
162
    }
163
164
    public function hasRequire($package)
165
    {
166
        $pman = $this->getPackageManager();
167
168
        return $pman && array_key_exists($package, $pman->getConfiguration()->getRequire());
169
    }
170
171
    public function hasRequireDev($package)
172
    {
173
        $pman = $this->getPackageManager();
174
175
        return $pman && array_key_exists($package, $pman->getConfiguration()->getRequireDev());
176
    }
177
}
178