Completed
Push — master ( 344091...41b0ff )
by Pablo
04:39
created

MessageConfigData   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 75
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A config() 0 7 1
A configMessages() 0 17 2
A setMessage() 0 5 1
1
<?php
2
3
namespace PhpGitHooks\Application\Message;
4
5
use Composer\IO\IOInterface;
6
7
class MessageConfigData
8
{
9
    const TOOL = 'message';
10
    const DEFAULT_RIGHT_MESSAGE = 'HEY, GOOD JOB!!';
11
    const DEFAULT_ERROR_MESSAGE = 'FIX YOUR FUCKING CODE!!';
12
    const KEY_RIGHT_MESSAGE = 'right-message';
13
    const KEY_ERROR_MESSAGE = 'error-message';
14
    /**
15
     * @var array
16
     */
17
    private $configData;
18
    /**
19
     * @var IOInterface
20
     */
21
    private $io;
22
    /**
23
     * @var string
24
     */
25
    private $hook;
26
27
    /**
28
     * MessageConfigData constructor.
29
     *
30
     * @param IOInterface $io
31
     * @param $hook
32
     */
33 9
    public function __construct(IOInterface $io, $hook)
34
    {
35 9
        $this->io = $io;
36 9
        $this->hook = $hook;
37 9
    }
38
39
    /**
40
     * @param array $data
41
     *
42
     * @return array
43
     */
44 9
    public function config(array $data)
45
    {
46 9
        $this->configData = $data;
47 9
        $this->configMessages();
48
49 9
        return $this->configData[self::TOOL];
50
    }
51
52 9
    private function configMessages()
53
    {
54 9
        if (!isset($this->configData[self::TOOL])) {
55 8
            $this->configData = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $configData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56 8
            $rightMessage = $this->setMessage(
57 8
                sprintf('Write a right message for %s hook:', $this->hook),
58
                self::DEFAULT_RIGHT_MESSAGE
59 8
            );
60 8
            $errorMessage = $this->setMessage(
61 8
                sprintf('Write a error message for %s hook:', $this->hook),
62
                self::DEFAULT_ERROR_MESSAGE
63 8
            );
64
65 8
            $this->configData[self::TOOL][self::KEY_RIGHT_MESSAGE] = $rightMessage;
66 8
            $this->configData[self::TOOL][self::KEY_ERROR_MESSAGE] = $errorMessage;
67 8
        }
68 9
    }
69
70
    /**
71
     * @param string $message
72
     * @param string $default
73
     *
74
     * @return string
75
     */
76 8
    private function setMessage($message, $default)
77
    {
78 8
        return strtoupper($this->io
79 8
            ->ask(sprintf('<info>%s</info> [<comment>%s</comment>]', $message, $default), $default));
80
    }
81
}
82