Passed
Push — main ( f0911f...cd99fe )
by Sebastian
05:24
created

Inspector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of CaptainHook.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\Template;
13
14
use CaptainHook\App\CH;
15
use CaptainHook\App\Console\IO;
16
use Exception;
17
use SebastianFeldmann\Git\Repository;
18
19
/**
20
 * Inspector
21
 *
22
 * @package CaptainHook
23
 * @author  Sebastian Feldmann <[email protected]>
24
 * @link    https://github.com/captainhookphp/captainhook
25
 * @since   Class available since Release 5.22.0
26
 */
27
class Inspector
28
{
29
    /**
30
     * @var string
31
     */
32
    private string $hook;
33
34
    /**
35
     * @var \CaptainHook\App\Console\IO
36
     */
37
    private IO $io;
38
39
    /**
40
     * @var \SebastianFeldmann\Git\Repository
41
     */
42
    private Repository $repository;
43
44
    /**
45
     * @param string                            $hook
46
     * @param \CaptainHook\App\Console\IO       $io
47
     * @param \SebastianFeldmann\Git\Repository $repo
48
     */
49 28
    public function __construct(string $hook, IO $io, Repository $repo)
50
    {
51 28
        $this->hook       = $hook;
52 28
        $this->io         = $io;
53 28
        $this->repository = $repo;
54
    }
55
56
    /**
57
     * Check if the hooks script needs an update
58
     *
59
     * @return void
60
     * @throws \Exception
61
     */
62 28
    public function inspect(): void
63
    {
64 28
        $path = $this->repository->getHooksDir() . '/' . $this->hook;
65 28
        if (!file_exists($path)) {
66 6
            $this->io->write('<fg=red>Can\'t read hook script at: ' . $path . '</>');
67 6
            throw new Exception('hook script not found: ' . $path);
68
        }
69
70 22
        $hookScript       = file_get_contents($path);
71 22
        $installerVersion = $this->detectInstallerVersion((string) $hookScript);
72
73
        // could not find any installer version
74
        // this is not optimal but if people decide to customise there is only so much I can do
75 22
        if (empty($installerVersion)) {
76 20
            return;
77
        }
78
79 2
        if (version_compare($installerVersion, CH::MIN_REQ_INSTALLER) < 0) {
80 1
            $this->io->write([
81 1
                '<fg=red>Warning: Hook script is out of date</>',
82 1
                'The git hook script needs to be updated.',
83 1
                'Required version is <info>' . CH::MIN_REQ_INSTALLER . '</info>'
84 1
                  . ' found <fg=red>' . $installerVersion . '</>.',
85 1
                'Please re-install your hook by running:',
86 1
                '  <comment>captainhook install ' . $this->hook . '</comment>',
87 1
                '',
88 1
                '<fg=red>captainhook failed executing your hooks</>',
89 1
            ]);
90 1
            throw new Exception('hook code out of date');
91
        }
92
    }
93
94
    /**
95
     * Try to find the version that installed the hook script
96
     *
97
     * @param  string $hookScript
98
     * @return string
99
     */
100 22
    private function detectInstallerVersion(string $hookScript): string
101
    {
102 22
        $version = '';
103 22
        $matches = [];
104
105 22
        if (preg_match('#installed by Captainhook ([0-9]+\.[0-9]+\.[0-9]+)#i', $hookScript, $matches)) {
106 2
            $version = $matches[1];
107
        }
108 22
        return $version;
109
    }
110
}
111