ProjectExtension   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 15
eloc 76
c 4
b 0
f 0
dl 0
loc 209
ccs 0
cts 41
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A repositoryLink() 0 24 1
A getFilters() 0 6 1
A getFunctions() 0 18 1
A __construct() 0 3 1
A getCurrentFavourites() 0 13 2
A branchLink() 0 21 2
A shaLink() 0 21 3
A humanDate() 0 15 4
1
<?php
2
3
namespace App\Twig;
4
5
use App\Facades\Security;
6
use App\Model\Project;
7
use App\Provider\Factory;
8
use App\Security\Manager;
9
use Carbon\Carbon;
10
use Ronanchilvers\Foundation\Traits\Optionable;
11
use Ronanchilvers\Orm\Orm;
12
use Ronanchilvers\Utility\Str;
13
use Twig\Extension\AbstractExtension;
14
use Twig\Markup;
15
use Twig\TwigFunction;
16
17
/**
18
 * Twig extension for github helper methods
19
 *
20
 * @author Ronan Chilvers <[email protected]>
21
 */
22
class ProjectExtension extends AbstractExtension
23
{
24
    /**
25
     * @var \App\Provider\Factory
26
     */
27
    protected $factory;
28
29
    /**
30
     * @var string
31
     */
32
    // protected $repoLinkHtml = '<span class="icon"><i class="fab fa-{provider}"></i></span><a class="button is-text" href="{user_url}" target="_blank"><span>{user}</span></a>/<a class="button is-text" href="{repo_url}" target="_blank">{repo}</a>';
33
    protected $repoLinkHtml = '<a href="{user_url}" target="_blank">{user}</a>/<a href="{repo_url}" target="_blank">{repo}</a>';
34
35
    /**
36
     * @var string
37
     */
38
    protected $branchLinkHtml = '<a href="{url}" target="_blank">{branch}</a>';
39
40
    /**
41
     * @var string
42
     */
43
    protected $shaLinkHtml = '<a href="{url}" target="_blank">{sha}</a>';
44
45
    /**
46
     * Class constructor
47
     *
48
     * @param \App\Provider\Factory $factory
49
     * @author Ronan Chilvers <[email protected]>
50
     */
51
    public function __construct(Factory $factory)
52
    {
53
        $this->factory = $factory;
54
    }
55
56
    /**
57
     * @author Ronan Chilvers <[email protected]>
58
     */
59
    public function getFunctions()
60
    {
61
        return [
62
            new TwigFunction(
63
                'repo_link',
64
                [$this, 'repositoryLink']
65
            ),
66
            new TwigFunction(
67
                'branch_link',
68
                [$this, 'branchLink']
69
            ),
70
            new TwigFunction(
71
                'sha_link',
72
                [$this, 'shaLink']
73
            ),
74
            new TwigFunction(
75
                'current_favourites',
76
                [$this, 'getCurrentFavourites']
77
            ),
78
        ];
79
    }
80
81
    /**
82
     * @author Ronan Chilvers <[email protected]>
83
     */
84
    public function getFilters()
85
    {
86
        return [
87
            new \Twig\TwigFilter(
88
                'human_date',
89
                [$this, 'humanDate']
90
            ),
91
        ];
92
    }
93
94
    /**
95
     * Get a repository link for a given project
96
     *
97
     * @param Project $project
98
     * @return string
99
     * @author Ronan Chilvers <[email protected]>
100
     */
101
    public function repositoryLink(Project $project)
102
    {
103
        $provider = $this->factory->forProject(
104
            $project
105
        );
106
        $bits = explode('/', $project->repository);
107
        $userUrl = $provider->getRepositoryLink(
108
            $bits[0]
109
        );
110
        $url = $provider->getRepositoryLink(
111
            $project->repository
112
        );
113
        $link = Str::moustaches(
114
            $this->repoLinkHtml,
115
            [
116
                'provider'   => $project->provider,
117
                'user_url'   => $userUrl,
118
                'user'       => $bits[0],
119
                'repo_url'   => $url,
120
                'repo'       => $bits[1],
121
            ]
122
        );
123
124
        return new Markup($link, 'UTF-8');
125
    }
126
127
    /**
128
     * Get a branch link for a given project
129
     *
130
     * @param Project $project
131
     * @return string
132
     * @author Ronan Chilvers <[email protected]>
133
     */
134
    public function branchLink(Project $project, string $branch = null)
135
    {
136
        if (is_null($branch)) {
137
            $branch = $project->branch;
138
        }
139
        $provider = $this->factory->forProject(
140
            $project
141
        );
142
        $url = $provider->getBranchLink(
143
            $project->repository,
144
            $branch
145
        );
146
        $link = Str::moustaches(
147
            $this->branchLinkHtml,
148
            [
149
                'branch' => $branch,
150
                'url'    => $url
151
            ]
152
        );
153
154
        return new Markup($link, 'UTF-8');
155
    }
156
157
    /**
158
     * Create a link for a given SHA and project
159
     *
160
     * @param Project $project
161
     * @param string $sha
162
     * @return string
163
     * @author Ronan Chilvers <[email protected]>
164
     */
165
    public function shaLink(Project $project = null, string $sha = null)
166
    {
167
        if (is_null($project) || is_null($sha)) {
168
            return 'N/A';
169
        }
170
        $provider = $this->factory->forProject(
171
            $project
172
        );
173
        $url = $provider->getShaLink(
174
            $project->repository,
175
            $sha
176
        );
177
        $link = Str::moustaches(
178
            $this->shaLinkHtml,
179
            [
180
                'sha' => substr($sha, 0, 8),
181
                'url' => $url
182
            ]
183
        );
184
185
        return new Markup($link, 'UTF-8');
186
    }
187
188
    /**
189
     * Generate a human readable datetime string from a Carbon object
190
     *
191
     * @param \Carbon\Carbon $carbon
192
     * @return string
193
     * @author Ronan Chilvers <[email protected]>
194
     */
195
    public function humanDate(Carbon $carbon = null)
196
    {
197
        if (is_null($carbon)) {
198
            return 'N/A';
199
        }
200
        // More than a day ago
201
        if (0 < $carbon->diffInDays()) {
202
            return $carbon->format('Y-m-d H:i:s');
203
        }
204
        // More than 30 minutes ago
205
        if (1440 < $carbon->diffInMinutes()) {
206
            return $carbon->format('H:i:s');
207
        }
208
209
        return $carbon->diffForHumans(['parts' => 2]);
210
    }
211
212
    /**
213
     * Get the favourite projects for the current user
214
     *
215
     * @return []
0 ignored issues
show
Documentation Bug introduced by
The doc comment [] at position 0 could not be parsed: Unknown type name '[' at position 0 in [].
Loading history...
216
     * @author Ronan Chilvers <[email protected]>
217
     */
218
    public function getCurrentFavourites()
219
    {
220
        $user = Security::user();
221
        $userFavourites = $user->preference(
222
            'favourites',
223
            []
224
        );
225
        if (empty($userFavourites)) {
226
            return [];
227
        }
228
229
        return Orm::finder(Project::class)->all(
230
            $userFavourites
0 ignored issues
show
Bug introduced by
It seems like $userFavourites can also be of type array; however, parameter $page of Ronanchilvers\Orm\Finder::all() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

230
            /** @scrutinizer ignore-type */ $userFavourites
Loading history...
231
        );
232
    }
233
}
234