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

ApplicationVersionViewComposer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 12.9 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 62
rs 10
wmc 8
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A compose() 0 7 2
A onBackend() 8 8 2
A getAppVersion() 0 6 2
A getComposerFile() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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