Completed
Push — 2.0 ( 260628...4fde74 )
by Nicolas
03:20
created

ApplicationVersionViewComposer::onBackend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 8
loc 8
rs 9.4285
1
<?php namespace Modules\Core\Composers;
2
3
use Illuminate\Contracts\Cache\Repository;
4
use Illuminate\Contracts\Filesystem\Filesystem;
5
use Illuminate\Contracts\View\View;
6
use Illuminate\Http\Request;
7
8
class ApplicationVersionViewComposer
9
{
10
    /**
11
     * @var Filesystem
12
     */
13
    private $filesystem;
14
    /**
15
     * @var Repository
16
     */
17
    private $cache;
18
    /**
19
     * @var Request
20
     */
21
    private $request;
22
23
    public function __construct(Filesystem $filesystem, Repository $cache, Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
24
    {
25
        $this->filesystem = $filesystem;
26
        $this->cache = $cache;
27
        $this->request = $request;
28
    }
29
30
    public function compose(View $view)
31
    {
32
        if ($this->onBackend() === false) {
33
            return;
34
        }
35
        $view->with('version', $this->getAppVersion());
36
    }
37
38 View Code Duplication
    private function onBackend()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $url = $this->request->url();
41
        if (str_contains($url, config('asgard.core.core.admin-prefix'))) {
42
            return true;
43
        }
44
        return false;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    private function getAppVersion()
51
    {
52
        $composerFile = $this->getComposerFile();
53
54
        return isset($composerFile->version) ? $composerFile->version : '1.0';
55
    }
56
57
    /**
58
     * Get the decoded contents from the main composer.json file
59
     * @return object
60
     */
61
    private function getComposerFile()
62
    {
63
        $composerFile = $this->cache->remember('app.version', 1440, function () {
64
            return $this->filesystem->get('composer.json');
65
        });
66
67
        return json_decode($composerFile);
68
    }
69
}
70