PackagingManager::pack()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 3
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Meetings\Attachments\Packaging;
29
30
use SplFileInfo;
31
32
final class PackagingManager
33
{
34
    /** @var AttachmentsPackerInterface[] */
35
    protected $packers = [];
36
37
    public function addPacker(AttachmentsPackerInterface $packer)
38
    {
39
        foreach ($packer->getSupportedFormats() as $format) {
40
            $this->packers[$format] = $packer;
41
        }
42
43
        return $this;
44
    }
45
46
    /**
47
     * @param array $attachments
48
     * @param string $format
49
     * @param string $filename
50
     * @return SplFileInfo
51
     * @throws FormatNotSupportedException
52
     */
53
    public function pack(array $attachments, $format = "zip", $filename = "")
54
    {
55
        if (! isset($this->packers[$format])) {
56
            throw new FormatNotSupportedException(sprintf(
57
                "The requested package format [%s] is not supported.",
58
                $format
59
            ));
60
        }
61
62
        $filename = ($filename != "") ? $filename : $this->generateFileName();
63
64
        return $this->packers[$format]->pack($attachments, $filename);
65
    }
66
67
    public function getPackers()
68
    {
69
        return $this->packers;
70
    }
71
72
    private function generateFileName()
73
    {
74
        return md5((new \DateTime())->getTimestamp() . rand(0, 1000));
75
    }
76
}
77