Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Packer
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getItemPacker
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 pack
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 getStorageDirectory
0.00% covered (danger)
0.00%
0 / 3
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) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\taoItems\model\pack;
23
24use core_kernel_classes_Resource;
25use oat\oatbox\filesystem\Directory;
26use taoItems_models_classes_ItemsService;
27use common_exception_NoImplementation;
28use common_Exception;
29use Exception;
30use Zend\ServiceManager\ServiceLocatorAwareInterface;
31use Zend\ServiceManager\ServiceLocatorAwareTrait;
32
33/**
34 * The Item Pack represents the item package data produced by the compilation.
35 *
36 * @package taoItems
37 * @author Bertrand Chevrier <bertrand@taotesting.com>
38 */
39class Packer implements ServiceLocatorAwareInterface
40{
41    use ServiceLocatorAwareTrait;
42
43    /**
44     * The item to pack
45     * @var core_kernel_classes_Resource
46     */
47    private $item;
48
49    /**
50     * The lang of the item to pack
51     * @var string
52     */
53    private $lang;
54
55    /**
56     * The item service
57     * @var taoItems_models_classes_ItemsService
58     */
59    private $itemService;
60
61    /** @var bool */
62    private $skipValidation;
63
64    /**
65     * Create a packer for an item
66     *
67     * @param core_kernel_classes_Resource $item
68     * @param string $lang
69     */
70    public function __construct(core_kernel_classes_Resource $item, $lang = '', bool $skipValidation = false)
71    {
72        $this->item = $item;
73        $this->lang = $lang;
74        $this->itemService = taoItems_models_classes_ItemsService::singleton();
75        $this->skipValidation = $skipValidation;
76    }
77
78    /**
79     * Get the packer for the item regarding it's implementation.
80     *
81     * @return ItemPacker the item packer implementation
82     * @throws common_exception_NoImplementation
83     */
84    protected function getItemPacker()
85    {
86
87        //look at the item model
88        $itemModel = $this->itemService->getItemModel($this->item);
89        if (is_null($itemModel)) {
90            throw new common_exception_NoImplementation('No item model for item ' . $this->item->getUri());
91        }
92
93        //get the itemModel implementation for this model
94        $impl = $this->itemService->getItemModelImplementation($itemModel);
95        if (is_null($impl)) {
96            throw new common_exception_NoImplementation('No implementation for model ' . $itemModel->getUri());
97        }
98
99        //then retrieve the packer class and instantiate it
100        $packerClass = $impl->getPackerClass();
101        if (is_null($packerClass) || get_parent_class($packerClass) !== 'oat\taoItems\model\pack\ItemPacker') {
102            throw new common_exception_NoImplementation('The packer class seems to be not implemented');
103        }
104
105        return new $packerClass();
106    }
107
108    /**
109     * Pack an item.
110     * @param array $assetEncoders the list of encoders to use in packing (configure the item packer)
111     * @param boolean $nestedResourcesInclusion
112     * @return ItemPack of the item. It can be serialized directly.
113     * @throws common_Exception
114     */
115    public function pack($assetEncoders = [], $nestedResourcesInclusion = true)
116    {
117
118        try {
119            //call the factory to get the itemPacker implementation
120            $itemPacker = $this->getItemPacker();
121
122            $itemPacker->setAssetEncoders($assetEncoders);
123            $itemPacker->setNestedResourcesInclusion($nestedResourcesInclusion);
124            $itemPacker->setSkipValidation($this->skipValidation);
125
126            //then create the pack
127            $itemPack = $itemPacker->packItem($this->item, $this->lang, $this->getStorageDirectory());
128        } catch (Exception $e) {
129            throw new common_Exception('The item ' . $this->item->getUri() . ' cannot be packed : ' . $e->getMessage());
130        }
131
132        return $itemPack;
133    }
134
135    /**
136     * @return Directory
137     * @throws \Exception
138     */
139    protected function getStorageDirectory()
140    {
141        /** @var \oat\oatbox\filesystem\Directory $directory */
142        $directory = $this->itemService->getItemDirectory($this->item, $this->lang);
143
144        //we should use be language unaware for storage manipulation
145        $path = str_replace($this->lang, '', $directory->getPrefix());
146
147        return $this->itemService->getDefaultItemDirectory()->getDirectory($path);
148    }
149}