ZipFile::__destruct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the Amplexor\XConnect library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/amplexor-drupal/xconnect/
7
 * @version 1.0.0
8
 * @package Amplexor.XConnect
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Amplexor\XConnect\Request\File;
15
16
use Amplexor\XConnect\Request;
17
use Amplexor\XConnect\Request\Encoder\XmlEncoder;
18
19
/**
20
 * Request as a Zip file.
21
 */
22
class ZipFile implements FileInterface
23
{
24
    /**
25
     * The filePath where the file is located.
26
     *
27
     * @var string
28
     */
29
    private $filePath;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param string $filePath
35
     *   The path where the file is located.
36
     */
37 18
    private function __construct($filePath)
38
    {
39 18
        $this->filePath = $filePath;
40 18
    }
41
42
    /**
43
     * Destructor.
44
     *
45
     * Destructing the file object will delete the file from the filesystem.
46
     */
47 18
    public function __destruct()
48
    {
49 18
        if (file_exists($this->filePath)) {
50 18
            unlink($this->filePath);
51 18
        }
52 18
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 21
    public static function create(Request $request, $directory)
58
    {
59 21
        $order = $request->getOrder();
60
61
        // Create the filename based on the order name.
62 21
        $zipFilePath = sprintf(
63 21
            '%s/%s.zip',
64 21
            rtrim(rtrim($directory), '/'),
65 21
            $order->getOrderName()
66 21
        );
67
68
        // Create a new ZipFile on disk.
69 21
        $zip = new \ZipArchive();
70 21
        $zip->open($zipFilePath, \ZipArchive::CREATE);
71
72
        // Add the order to the archive as an xml file.
73 21
        $encoder = new XmlEncoder();
74 21
        $zip->addFromString('order.xml', $encoder->encode($order));
75
76
        // Add the files to the archive.
77 21
        foreach ($request->getFiles() as $fileName => $filePath) {
78 21
            $zip->addFile($filePath, 'Input/' . $fileName);
79 21
        }
80
81
        // Add the content to the archive.
82 21
        foreach ($request->getContent() as $fileName => $content) {
83 21
            $zip->addFromString('Input/' . $fileName, $content);
84 21
        }
85
86
        // Close the file so the content gets compressed and the file is saved.
87 21
        $result = $zip->close();
88
89
        // Check if no errors.
90 21
        if ($result !== true || !file_exists($zipFilePath)) {
91 3
            throw new FileException(
92 3
                sprintf('Can\'t create the zip archive "%s"', $zipFilePath)
93 3
            );
94
        }
95
96 18
        return new static($zipFilePath);
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102 18
    public function getPath()
103
    {
104 18
        return $this->filePath;
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110 3
    public function getFileName()
111
    {
112 3
        return basename($this->getPath());
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118 3
    public function getDirectory()
119
    {
120 3
        return dirname($this->getPath());
121
    }
122
}
123