Passed
Push — master ( b5d685...4f7527 )
by Ronan
03:54
created

ProjectExtension::getCurrentFavourites()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
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)
135
    {
136
        $provider = $this->factory->forProject(
137
            $project
138
        );
139
        $url = $provider->getBranchLink(
140
            $project->repository,
141
            $project->branch
142
        );
143
        $link = Str::moustaches(
144
            $this->branchLinkHtml,
145
            [
146
                'branch' => $project->branch,
147
                'url'    => $url
148
            ]
149
        );
150
151
        return new Markup($link, 'UTF-8');
152
    }
153
154
    /**
155
     * Create a link for a given SHA and project
156
     *
157
     * @param Project $project
158
     * @param string $sha
159
     * @return string
160
     * @author Ronan Chilvers <[email protected]>
161
     */
162
    public function shaLink(Project $project = null, string $sha = null)
163
    {
164
        if (is_null($project) || is_null($sha)) {
165
            return 'N/A';
166
        }
167
        $provider = $this->factory->forProject(
168
            $project
169
        );
170
        $url = $provider->getShaLink(
171
            $project->repository,
172
            $sha
173
        );
174
        $link = Str::moustaches(
175
            $this->shaLinkHtml,
176
            [
177
                'sha' => substr($sha, 0, 8),
178
                'url' => $url
179
            ]
180
        );
181
182
        return new Markup($link, 'UTF-8');
183
    }
184
185
    /**
186
     * Generate a human readable datetime string from a Carbon object
187
     *
188
     * @param \Carbon\Carbon $carbon
189
     * @return string
190
     * @author Ronan Chilvers <[email protected]>
191
     */
192
    public function humanDate(Carbon $carbon = null)
193
    {
194
        if (is_null($carbon)) {
195
            return 'N/A';
196
        }
197
        // More than a day ago
198
        if (0 < $carbon->diffInDays()) {
199
            return $carbon->format('Y-m-d H:i:s');
200
        }
201
        // More than 30 minutes ago
202
        if (1440 < $carbon->diffInMinutes()) {
203
            return $carbon->format('H:i:s');
204
        }
205
206
        return $carbon->diffForHumans(['parts' => 2]);
207
    }
208
209
    /**
210
     * Get the favourite projects for the current user
211
     *
212
     * @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...
213
     * @author Ronan Chilvers <[email protected]>
214
     */
215
    public function getCurrentFavourites()
216
    {
217
        $user = Security::user();
218
        $userFavourites = $user->preference(
219
            'favourites',
220
            []
221
        );
222
        if (empty($userFavourites)) {
223
            return [];
224
        }
225
226
        return Orm::finder(Project::class)->all(
227
            $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

227
            /** @scrutinizer ignore-type */ $userFavourites
Loading history...
228
        );
229
    }
230
}
231