Completed
Push — master ( 6a89b8...99f163 )
by Greg
02:20
created

CommandInfoDeserializer::constructFromCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 12
nc 1
nop 2
1
<?php
2
namespace Consolidation\AnnotatedCommand\Parser;
3
4
use Symfony\Component\Console\Input\InputOption;
5
use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParser;
6
use Consolidation\AnnotatedCommand\Parser\Internal\CommandDocBlockParserFactory;
7
use Consolidation\AnnotatedCommand\AnnotationData;
8
9
/**
10
 * Deserialize a CommandInfo object
11
 */
12
class CommandInfoDeserializer
13
{
14
    // TODO: in a future version, move CommandInfo::deserialize here
15
    public function deserialize($data)
16
    {
17
        return CommandInfo::deserialize((array)$data);
18
    }
19
20
    protected static function cachedMethodExists($cache)
21
    {
22
        return method_exists($cache['class'], $cache['method_name']);
23
    }
24
25
    public static function isValidSerializedData($cache)
26
    {
27
        return
28
            isset($cache['schema']) &&
29
            isset($cache['method_name']) &&
30
            isset($cache['mtime']) &&
31
            ($cache['schema'] > 0) &&
32
            ($cache['schema'] <= CommandInfo::SERIALIZATION_SCHEMA_VERSION) &&
33
            self::cachedMethodExists($cache);
34
    }
35
36
    public function constructFromCache(CommandInfo $commandInfo, $info_array)
37
    {
38
        $info_array += $this->defaultSerializationData();
39
40
        $commandInfo
41
            ->setName($info_array['name'])
42
            ->replaceRawAnnotations($info_array['annotations'])
43
            ->setAliases($info_array['aliases'])
44
            ->setHelp($info_array['help'])
45
            ->setDescription($info_array['description'])
46
            ->replaceExampleUsages($info_array['example_usages'])
47
            ->setReturnType($info_array['return_type'])
48
            ;
49
50
        $this->constructDefaultsWithDescriptions($commandInfo->arguments(), (array)$info_array['arguments']);
51
        $this->constructDefaultsWithDescriptions($commandInfo->options(), (array)$info_array['options']);
52
    }
53
54
    protected function constructDefaultsWithDescriptions(DefaultsWithDescriptions $defaults, $data)
55
    {
56
        foreach ($data as $key => $info) {
57
            $info = (array)$info;
58
            $defaults->add($key, $info['description']);
59
            if (array_key_exists('default', $info)) {
60
                $defaults->setDefaultValue($key, $info['default']);
61
            }
62
        }
63
    }
64
65
66
    /**
67
     * Default data. Everything should be provided during serialization;
68
     * this is just as a fallback for unusual circumstances.
69
     * @return array
70
     */
71
    protected function defaultSerializationData()
72
    {
73
        return [
74
            'description' => '',
75
            'help' => '',
76
            'aliases' => [],
77
            'annotations' => [],
78
            'example_usages' => [],
79
            'return_type' => [],
80
            'parameters' => [],
81
            'arguments' => [],
82
            'options' => [],
83
            'mtime' => 0,
84
        ];
85
    }
86
}
87