Test Failed
Push — master ( ca159c...8f4401 )
by Sebastian
03:00
created

SerializedFile::factory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * @package Application Utils
4
 * @subpackage FileHelper
5
 * @see \AppUtils\FileHelper\SerializedFile
6
 */
7
8
declare(strict_types=1);
9
10
namespace AppUtils\FileHelper;
11
12
use AppUtils\FileHelper;
13
use AppUtils\FileHelper_Exception;
14
use SplFileInfo;
15
16
/**
17
 * Handles files containing data serialized with the
18
 * PHP function {@see serialize()}.
19
 *
20
 * Create an instance with {@see SerializedFile::factory()}.
21
 *
22
 * @package Application Utils
23
 * @subpackage FileHelper
24
 * @author Sebastian Mordziol <[email protected]>
25
 */
26
class SerializedFile extends FileInfo
27
{
28
    /**
29
     * @param string|PathInfoInterface|SplFileInfo $path
30
     * @return SerializedFile
31
     * @throws FileHelper_Exception
32
     */
33
    public static function factory($path) : SerializedFile
34
    {
35
        if($path instanceof self) {
36
            return $path;
37
        }
38
39
        $instance = self::createInstance($path);
40
41
        if($instance instanceof self) {
42
            return $instance;
43
        }
44
45
        throw new FileHelper_Exception(
46
            'Invalid class.'
47
        );
48
    }
49
50
    /**
51
     * Opens a serialized file and returns the unserialized data.
52
     * Only supports serialized arrays - classes are not allowed.
53
     *
54
     * @throws FileHelper_Exception
55
     * @return array<int|string,mixed>
56
     * @see FileHelper::parseSerializedFile()
57
     *
58
     * @see FileHelper::ERROR_FILE_DOES_NOT_EXIST
59
     * @see FileHelper::ERROR_SERIALIZED_FILE_CANNOT_BE_READ
60
     * @see FileHelper::ERROR_SERIALIZED_FILE_UNSERIALZE_FAILED
61
     */
62
    public function parse() : array
63
    {
64
        $contents = $this
65
            ->requireExists()
66
            ->requireReadable()
67
            ->getContents();
68
69
        $result = @unserialize(
70
            $contents,
71
            array(
72
                'allowed_classes' => false
73
            )
74
        );
75
76
        if($result !== false) {
77
            return $result;
78
        }
79
80
        throw new FileHelper_Exception(
81
            'Cannot unserialize the file contents.',
82
            sprintf(
83
                'Tried unserializing the data from file at [%s].',
84
                $this->getPath()
85
            ),
86
            FileHelper::ERROR_SERIALIZED_FILE_UNSERIALZE_FAILED
87
        );
88
    }
89
90
    /**
91
     * Saves the data serialized to the file.
92
     *
93
     * @param array<mixed> $data
94
     * @return $this
95
     * @throws FileHelper_Exception
96
     */
97
    public function putData(array $data) : self
98
    {
99
        $serialized = @serialize($data);
100
101
        return $this->putContents($serialized);
102
    }
103
}
104