ThemesFactory::__invoke()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 36
rs 8.439
cc 5
eloc 18
nc 16
nop 1
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
namespace LearnZF2Themes\Factory;
20
21
use Zend\ServiceManager\ServiceLocatorInterface;
22
23
/**
24
 * @author Stanimir Dimitrov <[email protected]>
25
 */
26
final class ThemesFactory
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function __invoke(ServiceLocatorInterface $serviceLocator)
32
    {
33
        $themesConfig = $serviceLocator->get('getThemesFromDir');
34
        $config = $serviceLocator->get('Config');
35
        $headScript = $serviceLocator->get('ViewHelperManager')->get('HeadScript');
36
        $headLink = $serviceLocator->get('ViewHelperManager')->get('headLink');
37
        $publicDir = '/themes/'.$config['theme']['name'].DIRECTORY_SEPARATOR;
38
39
        /*
40
         * Get theme name from config and load it.
41
         *
42
         * At this point the user has already been selected the new theme he wants to use
43
         * from indexAction.
44
         */
45
        $viewTemplate = $serviceLocator->get('ViewTemplatePathStack');
46
        $themes = $themesConfig['themes'][$config['theme']['name']];
47
48
        if (isset($themes['template_path_stack'])) {
49
            $viewTemplate->addPaths($themes['template_path_stack']);
50
        }
51
52
        if (isset($themes['template_map'])) {
53
            $viewTemplate = $serviceLocator->get('ViewTemplateMapResolver');
54
            $viewTemplate->merge($themes['template_map']);
55
        }
56
57
        foreach ($themes['css'] as $key => $file) {
58
            $headLink->prependStylesheet($publicDir.$file);
59
        }
60
61
        foreach ($themes['js'] as $key => $file) {
62
            $headScript->prependFile($publicDir.$file);
63
        }
64
65
        return $viewTemplate;
66
    }
67
}
68