ConsolePromptConflictHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handleConflict() 0 35 7
1
<?php
2
3
namespace Storeman\Cli\ConflictHandler;
4
5
use Storeman\Cli\ConsoleStyle;
6
use Storeman\ConflictHandler\ConflictHandlerInterface;
7
use Storeman\Exception;
8
use Storeman\Index\IndexObject;
9
10
/**
11
 * This implementation of a conflict handler asks the user to resolve it manually.
12
 */
13
class ConsolePromptConflictHandler implements ConflictHandlerInterface
14
{
15
    /**
16
     * @var ConsoleStyle
17
     */
18
    protected $consoleStyle;
19
20
    public function __construct(ConsoleStyle $consoleStyle)
21
    {
22
        $this->consoleStyle = $consoleStyle;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function handleConflict(IndexObject $remoteObject, IndexObject $localObject = null, IndexObject $lastLocalObject = null): int
29
    {
30
        if (!$this->consoleStyle->getInput()->isInteractive())
31
        {
32
            throw new Exception(sprintf("Trying to use conflict handler %s with non-interactive input", static::class));
33
        }
34
35
        $localObjectString = $localObject ? $localObject->__toString() : '-';
36
        $lastLocalObjectString = $lastLocalObject ? $lastLocalObject->__toString() : '-';
37
        $text = <<<TXT
38
<question>Encountered conflict at {$remoteObject->getRelativePath()}
39
Local: {$localObjectString}
40
Last local: {$lastLocalObjectString}
41
Remote: {$remoteObject}</question>
42
TXT;
43
44
        $this->consoleStyle->writeln($text);
45
46
        $return = null;
47
        do
48
        {
49
            $choice = $this->consoleStyle->choice('Would you like to use the local (l) or the remote (r) version?', ['l', 'r']);
50
51
            switch ($choice)
52
            {
53
                case 'l': $return = ConflictHandlerInterface::USE_LOCAL; break 2;
54
                case 'r': $return = ConflictHandlerInterface::USE_REMOTE; break 2;
55
            }
56
57
            $this->consoleStyle->warning("Invalid choice");
58
        }
59
        while(true);
60
61
        return $return;
62
    }
63
}
64