Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_models_classes_Compiler
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 9
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getStorage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getResource
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 spawnPublicDirectory
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 spawnPrivateDirectory
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fail
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getSubCompilerClass
n/a
0 / 0
n/a
0 / 0
0
 subCompile
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 getContext
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setContext
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 compile
n/a
0 / 0
n/a
0 / 0
0
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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22use oat\tao\model\service\ServiceFileStorage;
23
24/**
25 * An abstract compiler
26 *
27 * @access public
28 * @author Joel Bout, <joel@taotesting.com>
29 * @package taoDelivery
30 */
31abstract class tao_models_classes_Compiler implements \Zend\ServiceManager\ServiceLocatorAwareInterface
32{
33    use \Zend\ServiceManager\ServiceLocatorAwareTrait;
34
35    /**
36     * Resource to be compiled
37     * @var core_kernel_classes_Resource
38     */
39    private $resource;
40
41    /**
42     * @var tao_models_classes_service_FileStorage
43     */
44    private $compilationStorage = null;
45
46    /**
47     * A context object the compiler can use
48     * @var mixed
49     */
50    private $context;
51
52    /**
53     * @param core_kernel_classes_Resource $resource
54     * @param ServiceFileStorage $storage
55     */
56    public function __construct(core_kernel_classes_Resource $resource, ServiceFileStorage $storage)
57    {
58        $this->resource = $resource;
59        $this->compilationStorage = $storage;
60    }
61
62    /**
63     * Returns the storage to be used during compilation
64     *
65     * @return tao_models_classes_service_FileStorage
66     */
67    protected function getStorage()
68    {
69        return $this->compilationStorage;
70    }
71
72    /**
73     * @return core_kernel_classes_Resource
74     */
75    protected function getResource()
76    {
77        return $this->resource;
78    }
79
80    /**
81     * Returns a directory that is accessible to the client
82     *
83     * @return tao_models_classes_service_StorageDirectory
84     */
85    protected function spawnPublicDirectory()
86    {
87        return $this->compilationStorage->spawnDirectory(true);
88    }
89
90    /**
91     * Returns a directory that is not accessible to the client
92     *
93     * @return tao_models_classes_service_StorageDirectory
94     */
95    protected function spawnPrivateDirectory()
96    {
97        return $this->compilationStorage->spawnDirectory(false);
98    }
99
100    /**
101     * helper to create a fail report
102     *
103     * @param string $userMessage
104     * @return common_report_Report
105     */
106    protected function fail($userMessage)
107    {
108        return new common_report_Report(
109            common_report_Report::TYPE_ERROR,
110            $userMessage
111        );
112    }
113
114    /**
115     * Determin the compiler of the resource
116     *
117     *
118     * @param core_kernel_classes_Resource $resource
119     * @return string the name of the compiler class
120     */
121    abstract protected function getSubCompilerClass(core_kernel_classes_Resource $resource);
122
123    /**
124     * Compile a subelement of the current resource
125     *
126     * @param core_kernel_classes_Resource $resource
127     * @return common_report_Report returns a report that if successful contains the service call
128     */
129    protected function subCompile(core_kernel_classes_Resource $resource)
130    {
131        $compilerClass = $this->getSubCompilerClass($resource);
132        if (!class_exists($compilerClass)) {
133            common_Logger::e('Class ' . $compilerClass . ' not found while instanciating Compiler');
134            return $this->fail(__('%s is of a type that cannot be published', $resource->getLabel()));
135        }
136        if (!is_subclass_of($compilerClass, __CLASS__)) {
137            common_Logger::e('Compiler class ' . $compilerClass . ' is not a compiler');
138            return $this->fail(__('%s is of a type that cannot be published', $resource->getLabel()));
139        }
140        /** @var $compiler tao_models_classes_Compiler */
141        $compiler = new $compilerClass($resource, $this->getStorage());
142        $compiler->setServiceLocator($this->getServiceLocator());
143        $compiler->setContext($this->getContext());
144        $report = $compiler->compile();
145        return $report;
146    }
147
148    /**
149     * Gets the context object the compiler can use
150     * @return mixed
151     */
152    public function getContext()
153    {
154        return $this->context;
155    }
156
157    /**
158     * Sets the context object the compiler can use
159     * @param mixed $context
160     * @return tao_models_classes_Compiler
161     */
162    public function setContext($context)
163    {
164        $this->context = $context;
165        return $this;
166    }
167
168    /**
169     * Compile the resource into a runnable service
170     * and returns a report that if successful contains the service call
171     *
172     * @return common_report_Report
173     * @throws tao_models_classes_CompilationFailedException
174     */
175    abstract public function compile();
176}