Completed
Push — master ( 7efae6...e19db8 )
by Sebastian
03:17
created

CheckLockFile::loadFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
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
     * Executes the action.
29
     *
30
     * @param  \SebastianFeldmann\CaptainHook\Config         $config
31
     * @param  \SebastianFeldmann\CaptainHook\Console\IO     $io
32
     * @param  \SebastianFeldmann\Git\Repository             $repository
33
     * @param  \SebastianFeldmann\CaptainHook\Config\Action  $action
34
     * @throws \Exception
35
     */
36 4
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
37
    {
38 4
        $path           = $action->getOptions()->get('path', getcwd());
39 4
        $lockFileHash   = $this->getLockFileHash($path);
40 2
        $configFileHash = $this->getConfigFileHash($path);
41
42 2
        if ($lockFileHash !== $configFileHash) {
43 1
            throw new \Exception('composer.lock is out of date');
44
        }
45
46 1
        $io->write('<info>composer.lock is up to date</info>');
47 1
    }
48
49
    /**
50
     * Read the composer.lock file and extract the composer.json hash.
51
     *
52
     * @param  string $path
53
     * @return string
54
     * @throws \Exception
55
     */
56 4
    private function getLockFileHash(string $path) : string
57
    {
58 4
        $lockFile = json_decode($this->loadFile($path . DIRECTORY_SEPARATOR . 'composer.lock'));
59 3
        $hashKey  = 'content-hash';
60
61 3
        if (!isset($lockFile->$hashKey)) {
62 1
            throw new \Exception('could not find content hash, please update composer to the latest version');
63
        }
64
65 2
        return $lockFile->$hashKey;
66
    }
67
68
    /**
69
     * Read the composer.json file and create a md5 hash on its relevant content.
70
     *
71
     * @param  string $path
72
     * @return string
73
     */
74 2
    private function getConfigFileHash(string $path) : string
75
    {
76
        // this is composer internal code to generate the content-hash
77 2
        $content      = json_decode($this->loadFile($path . DIRECTORY_SEPARATOR . 'composer.json'), true);
78
        $relevantKeys = [
79 2
            'name',
80
            'version',
81
            'require',
82
            'require-dev',
83
            'conflict',
84
            'replace',
85
            'provide',
86
            'minimum-stability',
87
            'prefer-stable',
88
            'repositories',
89
            'extra',
90
        ];
91 2
        $relevantContent = [];
92 2
        foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
93 2
            $relevantContent[$key] = $content[$key];
94
        }
95 2
        if (isset($content['config']['platform'])) {
96
            $relevantContent['config']['platform'] = $content['config']['platform'];
97
        }
98 2
        ksort($relevantContent);
99
100 2
        return md5(json_encode($relevantContent));
101
    }
102
103
    /**
104
     * Load a composer file.
105
     *
106
     * @param  string $file
107
     * @return string
108
     * @throws \Exception
109
     */
110 4
    private function loadFile(string $file) : string
111
    {
112 4
        if (!file_exists($file)) {
113 1
            throw new \Exception($file . ' not found');
114
        }
115 3
        return file_get_contents($file);
116
    }
117
}
118