Completed
Push — master ( aa9657...bb9832 )
by Sebastian
05:21
created

CheckLockFile   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 63
c 0
b 0
f 0
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 12 2
A getLockFileHash() 0 6 1
A getConfigFileHash() 0 4 1
A loadFile() 0 7 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 3
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
37
    {
38 3
        $path           = $action->getOptions()->get('path', getcwd());
39 3
        $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
     */
55 3
    private function getLockFileHash(string $path) : string
56
    {
57 3
        $lockFile = json_decode($this->loadFile($path . DIRECTORY_SEPARATOR . 'composer.lock'));
58
59 2
        return $lockFile->hash;
60
    }
61
62
    /**
63
     * Read the composer.json file and create a md5 hash on its contents.
64
     *
65
     * @param  string $path
66
     * @return string
67
     */
68 2
    private function getConfigFileHash(string $path) : string
69
    {
70 2
        return md5($this->loadFile($path . DIRECTORY_SEPARATOR . 'composer.json'));
71
    }
72
73
    /**
74
     * Load a composer file.
75
     *
76
     * @param  string $file
77
     * @return string
78
     * @throws \Exception
79
     */
80 3
    private function loadFile(string $file) : string
81
    {
82 3
        if (!file_exists($file)) {
83 1
            throw new \Exception($file . ' not found');
84
        }
85 2
        return file_get_contents($file);
86
    }
87
}
88