Completed
Push — master ( afdf8f...03c0bd )
by
unknown
05:18
created

JsonFileAbstractExport::rewind()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 12
c 2
b 1
f 0
dl 0
loc 24
rs 9.5555
cc 5
nc 3
nop 0
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\ImportExport\Export;
14
15
use SplFileObject;
16
17
/**
18
 * Class JsonFileAbstractExport
19
 * @package Thelia\ImportExport\Export
20
 * @author Florian Bernard <[email protected]>
21
 */
22
abstract class JsonFileAbstractExport extends AbstractExport
0 ignored issues
show
Deprecated Code introduced by
The class Thelia\ImportExport\Export\AbstractExport has been deprecated: since 2.4, please use a specific AbstractExport (like JsonFileAbstractExport). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

22
abstract class JsonFileAbstractExport extends /** @scrutinizer ignore-deprecated */ AbstractExport
Loading history...
23
{
24
    /**
25
     * @var SplFileObject Data to export
26
     */
27
    private $data;
28
29
    public function current()
30
    {
31
        /** @var resource $file */
32
        $result = json_decode($this->data->current(), true);
33
34
        if ($result !== null) {
35
            return $result;
36
        }
37
38
        return [];
39
    }
40
41
    public function key()
42
    {
43
        return $this->data->key();
44
    }
45
46
    public function next()
47
    {
48
        $this->data->next();
49
    }
50
51
    public function rewind()
52
    {
53
        if ($this->data === null) {
54
            $data = $this->getData();
55
56
            // Check if $data is a path to a json file
57
            if (\is_string($data)
58
                && '.json' === substr($data, -5)
59
                && file_exists($data)
60
            ) {
61
                $this->data = new \SplFileObject($data, 'r');
62
                $this->data->setFlags(SPLFileObject::READ_AHEAD);
63
64
                $this->data->rewind();
65
66
                return;
67
            }
68
69
            throw new \DomainException(
70
                'Data shoul de a JSON file, ending with .json'
71
            );
72
        }
73
74
        throw new \LogicException('Export data can\'t be rewinded');
75
    }
76
77
    public function valid()
78
    {
79
        return $this->data->valid();
80
    }
81
82
    /**
83
     * Apply order and aliases on data
84
     *
85
     * @param array $data Raw data
86
     *
87
     * @return array Ordered and aliased data
88
     */
89
    public function applyOrderAndAliases(array $data)
90
    {
91
        if ($this->orderAndAliases === null) {
92
            return $data;
93
        }
94
95
        $processedData = [];
96
97
        foreach ($this->orderAndAliases as $key => $value) {
98
            if (\is_int($key)) {
99
                $fieldName = $value;
100
                $fieldAlias = $value;
101
            } else {
102
                $fieldName = $key;
103
                $fieldAlias = $value;
104
            }
105
106
            $processedData[$fieldAlias] = null;
107
            if (array_key_exists($fieldName, $data)) {
108
                $processedData[$fieldAlias] = $data[$fieldName];
109
            }
110
        }
111
112
        return $processedData;
113
    }
114
}
115