Completed
Push — master ( e19db8...16d393 )
by Sebastian
01:33
created

CheckLockFile::getLockFileHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\CaptainHook\Hook\Composer\Action;
11
12
use SebastianFeldmann\CaptainHook\Config;
13
use SebastianFeldmann\CaptainHook\Console\IO;
14
use SebastianFeldmann\CaptainHook\Hook\Action;
15
use SebastianFeldmann\Git\Repository;
16
17
/**
18
 * Class CheckLockFile
19
 *
20
 * @package CaptainHook
21
 * @author  Sebastian Feldmann <[email protected]>
22
 * @link    https://github.com/sebastianfeldmann/captainhook
23
 * @since   Class available since Release 1.0.1
24
 */
25
class CheckLockFile implements Action
26
{
27
    /**
28
     * Composer configuration keys that are relevant for the 'content-hash' creation.
29
     *
30
     * @var array
31
     */
32
    private $relevantKeys    = [
33
        'name',
34
        'version',
35
        'require',
36
        'require-dev',
37
        'conflict',
38
        'replace',
39
        'provide',
40
        'minimum-stability',
41
        'prefer-stable',
42
        'repositories',
43
        'extra',
44
    ];
45
46
    /**
47
     * Executes the action.
48
     *
49
     * @param  \SebastianFeldmann\CaptainHook\Config         $config
50
     * @param  \SebastianFeldmann\CaptainHook\Console\IO     $io
51
     * @param  \SebastianFeldmann\Git\Repository             $repository
52
     * @param  \SebastianFeldmann\CaptainHook\Config\Action  $action
53
     * @throws \Exception
54
     */
55 4
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
56
    {
57 4
        $path           = $action->getOptions()->get('path', getcwd());
58 4
        $lockFileHash   = $this->getLockFileHash($path);
59 2
        $configFileHash = $this->getConfigFileHash($path);
60
61 2
        if ($lockFileHash !== $configFileHash) {
62 1
            throw new \Exception('composer.lock is out of date');
63
        }
64
65 1
        $io->write('<info>composer.lock is up to date</info>');
66 1
    }
67
68
    /**
69
     * Read the composer.lock file and extract the composer.json hash.
70
     *
71
     * @param  string $path
72
     * @return string
73
     * @throws \Exception
74
     */
75 4
    private function getLockFileHash(string $path) : string
76
    {
77 4
        $lockFile = json_decode($this->loadFile($path . DIRECTORY_SEPARATOR . 'composer.lock'));
78 3
        $hashKey  = 'content-hash';
79
80 3
        if (!isset($lockFile->$hashKey)) {
81 1
            throw new \Exception('could not find content hash, please update composer to the latest version');
82
        }
83
84 2
        return $lockFile->$hashKey;
85
    }
86
87
    /**
88
     * Read the composer.json file and create a md5 hash on its relevant content.
89
     * This more or less is composer internal code to generate the content-hash
90
     *
91
     * @param  string $path
92
     * @return string
93
     */
94 2
    private function getConfigFileHash(string $path) : string
95
    {
96 2
        $content         = json_decode($this->loadFile($path . DIRECTORY_SEPARATOR . 'composer.json'), true);
97 2
        $relevantContent = [];
98
99 2
        foreach (array_intersect($this->relevantKeys, array_keys($content)) as $key) {
100 2
            $relevantContent[$key] = $content[$key];
101
        }
102 2
        if (isset($content['config']['platform'])) {
103
            $relevantContent['config']['platform'] = $content['config']['platform'];
104
        }
105 2
        ksort($relevantContent);
106
107 2
        return md5(json_encode($relevantContent));
108
    }
109
110
    /**
111
     * Load a composer file.
112
     *
113
     * @param  string $file
114
     * @return string
115
     * @throws \Exception
116
     */
117 4
    private function loadFile(string $file) : string
118
    {
119 4
        if (!file_exists($file)) {
120 1
            throw new \Exception($file . ' not found');
121
        }
122 3
        return file_get_contents($file);
123
    }
124
}
125