Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.92% covered (warning)
76.92%
10 / 13
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
LocalAssetHandler
76.92% covered (warning)
76.92%
10 / 13
40.00% covered (danger)
40.00%
2 / 5
7.60
0.00% covered (danger)
0.00%
0 / 1
 isApplicable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 getItemSource
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setItemSource
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 finalize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\taoQtiItem\model\qti\asset\handler;
23
24use oat\taoItems\model\media\LocalItemSource;
25
26class LocalAssetHandler implements AssetHandler
27{
28    /**
29     * @var LocalItemSource
30     */
31    protected $itemSource;
32
33    /**
34     * Always return true, local source is the last fallback
35     *
36     * @param $relativePath
37     * @throws \common_Exception
38     * @return bool
39     */
40    public function isApplicable($relativePath)
41    {
42        return true;
43    }
44
45    /**
46     * Handle the process to add file from $itemSource->add()
47     *
48     * @param $absolutePath
49     * @param $relativePath
50     * @return array
51     * @throws \common_Exception
52     * @throws \common_exception_Error
53     */
54    public function handle($absolutePath, $relativePath)
55    {
56        if (!$this->itemSource) {
57            throw new \common_Exception('Missing required parameter: item source');
58        }
59
60        // store locally, in a safe directory
61        $safePath = '';
62        if (dirname($relativePath) !== '.') {
63            $safePath = str_replace('../', '', dirname($relativePath)) . '/';
64        }
65
66        $info = $this->itemSource->add($absolutePath, basename($absolutePath), $safePath);
67        \common_Logger::i('Asset file \'' . $absolutePath . '\' copied.');
68        return $info;
69    }
70
71    /**
72     * Getter of $itemSource
73     * @return LocalItemSource
74     */
75    public function getItemSource()
76    {
77        return $this->itemSource;
78    }
79
80    /**
81     * Setter of itemSource
82     * @param LocalItemSource $itemSource
83     * @return $this
84     */
85    public function setItemSource(LocalItemSource $itemSource)
86    {
87        $this->itemSource = $itemSource;
88        return $this;
89    }
90
91    /**
92     * @inherit
93     */
94    public function finalize()
95    {
96        // Nothing to do
97    }
98}