ApplicationVersionViewComposer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
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
7
class ApplicationVersionViewComposer
8
{
9
    /**
10
     * @var Filesystem
11
     */
12
    private $filesystem;
13
    /**
14
     * @var Repository
15
     */
16
    private $cache;
17
18
    public function __construct(Filesystem $filesystem, Repository $cache)
19
    {
20
        $this->filesystem = $filesystem;
21
        $this->cache = $cache;
22
    }
23
24
    public function compose(View $view)
25
    {
26
        $view->with('version', $this->getAppVersion());
27
    }
28
29
    /**
30
     * @return string
31
     */
32
    private function getAppVersion()
33
    {
34
        $composerFile = $this->getComposerFile();
35
36
        return isset($composerFile->version) ? $composerFile->version : '1.0';
37
    }
38
39
    /**
40
     * Get the decoded contents from the main composer.json file
41
     * @return object
42
     */
43
    private function getComposerFile()
44
    {
45
        $composerFile = $this->cache->remember('app.version', 1440, function () {
46
            return $this->filesystem->get('composer.json');
47
        });
48
49
        return json_decode($composerFile);
50
    }
51
}
52