Passed
Push — master ( 41a727...ec7c33 )
by Sergey
02:52
created

PlainSerializer::serializeCommon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date: 3/3/16
5
 * @time: 5:13 PM
6
 */
7
8
namespace LTDBeget\sphinx\configurator\serializers;
9
10
11
use LTDBeget\sphinx\configurator\Configuration;
12
use LTDBeget\sphinx\configurator\configurationEntities\base\Section;
13
14
/**
15
 * Class PlainSerializer
16
 * serialize Configuration object to string for file .conf
17
 * @package LTDBeget\sphinx\configurator\serializers
18
 */
19
final class PlainSerializer
20
{
21
    /**
22
     * Make plain content for sphinx configuration file from Configuration object
23
     * @param Configuration $configuration
24
     * @return string
25
     */
26 2
    public static function serialize(Configuration $configuration) : string
27
    {
28 2
        $serializer         = new self();
29 2
        $serializer->object = $configuration;
30
31 2
        return $serializer->serializeInternal();
32
    }
33
34
    /**
35
     * @internal
36
     * @return string
37
     */
38 2
    public function serializeInternal() : string
39
    {
40 2
        $this->serializeSources();
41 2
        $this->serializeIndexes();
42 2
        $this->serializeIndexer();
43 2
        $this->serializeSearchd();
44 2
        $this->serializeCommon();
45
46 2
        return $this->string;
47
    }
48
49
    /**
50
     * @internal
51
     */
52 2
    private function serializeSources()
53
    {
54 2
        foreach ($this->object->iterateSource() as $source) {
55 2
            $this->serializeSection($source);
56
        }
57 2
    }
58
59
    /**
60
     * @internal
61
     */
62 2
    private function serializeIndexes()
63
    {
64 2
        foreach ($this->object->iterateIndex() as $index) {
65 2
            $this->serializeSection($index);
66
        }
67 2
    }
68
69
    /**
70
     * @internal
71
     */
72 2
    private function serializeIndexer()
73
    {
74 2
        if ($this->object->isHasIndexer()) {
75 2
            $this->serializeSection($this->object->getIndexer());
76
        }
77 2
    }
78
79
    /**
80
     * @internal
81
     */
82 2
    private function serializeSearchd()
83
    {
84 2
        if ($this->object->isHasSearchd()) {
85 2
            $this->serializeSection($this->object->getSearchd());
86
        }
87 2
    }
88
89
    /**
90
     * @internal
91
     */
92 2
    private function serializeCommon()
93
    {
94 2
        if ($this->object->isHasCommon()) {
95 2
            $this->serializeSection($this->object->getCommon());
96
        }
97 2
    }
98
99
    /**
100
     * @internal
101
     * @param Section $section
102
     */
103 2
    private function serializeSection(Section $section)
104
    {
105 2
        if($section->isDeleted()) {
106 1
            return;
107
        }
108
109 2
        $this->string .= "{$section}" . PHP_EOL;
110 2
        $this->string .= "{" . PHP_EOL;
111 2
        foreach ($section->iterateOptions() as $option) {
112 1
            $this->string .= "\t{$option}" . PHP_EOL;
113
        }
114 2
        $this->string .= "}" . PHP_EOL . PHP_EOL;
115 2
    }
116
117
    /**
118
     * @internal
119
     * ArrayDeserializer constructor.
120
     */
121 2
    private function __construct()
122
    {
123 2
    }
124
125
    /**
126
     * @var string
127
     */
128
    private $string = "";
129
130
    /**
131
     * @var Configuration
132
     */
133
    private $object;
134
}