Passed
Push — master ( 972705...1d9477 )
by Alexey
01:46
created

Writer::addShop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace iamsaint\yml;
4
5
use DateTimeImmutable;
6
use Exception;
7
use XMLWriter;
8
9
/**
10
 * Class Writer
11
 * @package iamsaint\yml
12
 */
13
class Writer
14
{
15
    /**
16
     * @param string $fileName
17
     * @param BaseObject $object
18
     * @return bool|int
19
     * @throws Exception
20
     */
21
    public function write(string $fileName, BaseObject $object)
22
    {
23
        return file_put_contents($fileName, $this->writeToString($object));
24
    }
25
26
    /**
27
     * @param BaseObject $object
28
     * @return mixed
29
     * @throws Exception
30
     */
31
    private function writeToString($object): string
32
    {
33
        $writer = new XMLWriter();
34
        $writer->openMemory();
35
        $writer->startDocument('1.0', 'UTF-8');
36
        $writer->setIndent(true);
37
        $writer->startElement('yml_catalog');
38
        $writer->writeAttribute('date', (new DateTimeImmutable('now'))->format('Y-m-d H:i'));
39
40
        if (null !== $object) {
41
            $object->write($writer);
42
        }
43
44
        $writer->endElement();
45
        return $writer->flush();
46
    }
47
}
48