Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
TempFlyStorageAssociation
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
11 / 11
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUpload
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addLocalCopies
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getLocalCopies
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getHashedKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeFiles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getKvStorage
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 set
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 remove
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getPrefix
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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) 2017 (original work) Open Assessment Technologies SA
19 *
20 */
21
22namespace oat\tao\model\upload;
23
24use oat\generis\persistence\PersistenceManager;
25use oat\oatbox\filesystem\File;
26use Zend\ServiceManager\ServiceLocatorAwareInterface;
27use Zend\ServiceManager\ServiceLocatorAwareTrait;
28use Zend\ServiceManager\ServiceLocatorInterface;
29
30class TempFlyStorageAssociation implements TmpLocalAwareStorageInterface, ServiceLocatorAwareInterface
31{
32    use ServiceLocatorAwareTrait;
33
34    /**
35     * @param ServiceLocatorInterface $serviceLocator
36     */
37    public function __construct(ServiceLocatorInterface $serviceLocator)
38    {
39        $this->setServiceLocator($serviceLocator);
40    }
41
42    /**
43     * @param File $file
44     * @throws \common_Exception
45     */
46    public function setUpload(File $file)
47    {
48        $hash = $this->getHashedKey($file);
49        $this->set($hash, []);
50    }
51
52    /**
53     * @param File $remote
54     * @param string $local
55     * @throws \common_Exception
56     */
57    public function addLocalCopies(File $remote, $local)
58    {
59        $hash = $this->getHashedKey($remote);
60        $current = $this->getLocalCopies($remote);
61        $current[] = $local;
62        $this->set($hash, $current);
63    }
64
65    /**
66     * @param $file
67     * @return array
68     */
69    public function getLocalCopies(File $file)
70    {
71        $result = $this->get($this->getHashedKey($file));
72        return is_array($result) ? $result : [];
73    }
74
75
76    /**
77     * @param File $file
78     * @return string
79     */
80    private function getHashedKey(File $file)
81    {
82        return md5($file->getPrefix());
83    }
84
85    /**
86     * @param File $file
87     */
88    public function removeFiles(File $file)
89    {
90        $this->remove($this->getHashedKey($file));
91    }
92
93    /**
94     * @return \common_persistence_KeyValuePersistence
95     */
96    private function getKvStorage()
97    {
98        /** @var PersistenceManager $pm */
99        $pm = $this->getServiceLocator()->get(PersistenceManager::SERVICE_ID);
100        return $pm->getPersistenceById('default_kv');
101    }
102
103    /**
104     * @param $key
105     * @param $val
106     * @throws \common_Exception
107     */
108    private function set($key, $val)
109    {
110        $key = $this->getPrefix() . $key;
111        $this->getKvStorage()->set($key, json_encode($val));
112    }
113
114    /**
115     * @param $key
116     */
117    private function remove($key)
118    {
119        $key = $this->getPrefix() . $key;
120        $this->getKvStorage()->del($key);
121    }
122
123    /**
124     * @param $key
125     * @return mixed|null
126     */
127    private function get($key)
128    {
129        $key = $this->getPrefix() . $key;
130        if ($this->getKvStorage()->exists($key)) {
131            return json_decode($this->getKvStorage()->get($key), true);
132        }
133        return null;
134    }
135
136    /**
137     * @return string
138     */
139    private function getPrefix()
140    {
141        return static::class;
142    }
143}