Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
8 / 20
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ServiceManagerAwareTrait
40.00% covered (danger)
40.00%
8 / 20
33.33% covered (danger)
33.33%
1 / 3
43.10
0.00% covered (danger)
0.00%
0 / 1
 getServiceManager
42.86% covered (danger)
42.86%
3 / 7
0.00% covered (danger)
0.00%
0 / 1
4.68
 registerService
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 propagate
27.27% covered (danger)
27.27%
3 / 11
0.00% covered (danger)
0.00%
0 / 1
19.85
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) 2014-2021 (original work) Open Assessment Technologies SA;
19 */
20
21namespace oat\oatbox\service;
22
23use oat\oatbox\log\LoggerService;
24use oat\oatbox\log\TaoLoggerAwareInterface;
25use Psr\Log\LoggerAwareInterface;
26use oat\oatbox\service\exception\InvalidServiceManagerException;
27use Zend\ServiceManager\ServiceLocatorAwareInterface;
28use Zend\ServiceManager\ServiceLocatorAwareTrait;
29
30/**
31 * Class ServiceManagerAwareTrait
32 *
33 * Trait to transport oat\oatbox\service\ServiceManager
34 * It includes tools to register and propagate oat service
35 *
36 * @package oat\oatbox\service
37 * @author Moyon Camille
38 * @deprecated New services must be registered using Dependency Injection Container
39 */
40trait ServiceManagerAwareTrait
41{
42    use ServiceLocatorAwareTrait;
43
44    /**
45     * Get the oat service manager.
46     *
47     * It should be used for service building, register, build, propagate
48     * For reading operation please use $this->getServiceLocator() instead
49     *
50     * @return ServiceManager
51     * @throws InvalidServiceManagerException
52     * @deprecated New services must be registered using Dependency Injection Container
53     */
54    public function getServiceManager()
55    {
56        $serviceManager = $this->getServiceLocator();
57        if ($serviceManager instanceof ServiceManager) {
58            return $serviceManager;
59        }
60        $msg = is_null($serviceManager)
61            ? 'ServiceLocator not initialized for ' . get_class($this)
62            : 'Alternate service locator not compatible with getServiceManager() in ' . __CLASS__;
63        throw new InvalidServiceManagerException($msg);
64    }
65
66    /**
67     * Register a service through ServiceManager
68     *
69     * @param $serviceKey
70     * @param ConfigurableService $service
71     * @param bool $allowOverride
72     * @throws \common_Exception
73     * @deprecated New services must be registered using Dependency Injection Container
74     */
75    public function registerService($serviceKey, ConfigurableService $service, $allowOverride = true)
76    {
77        if ($allowOverride || ! $this->getServiceLocator()->has($serviceKey)) {
78            $this->getServiceManager()->register($serviceKey, $service);
79        }
80    }
81
82    /**
83     * Propagate service dependencies
84     *
85     * @param $service
86     * @return mixed
87     * @deprecated New services must be registered using Dependency Injection Container
88     */
89    protected function propagate($service)
90    {
91        // Propagate the service manager
92        if ($service instanceof ServiceLocatorAwareInterface) {
93            $service->setServiceLocator($this->getServiceLocator());
94        }
95
96        // Propagate the logger service
97        if ($service instanceof LoggerAwareInterface) {
98            $logger = null;
99            if ($this instanceof TaoLoggerAwareInterface) {
100                $logger = $this->getLogger();
101            } else {
102                if ($this->getServiceLocator()->has(LoggerService::SERVICE_ID)) {
103                    $logger = $this->getServiceLocator()->get(LoggerService::SERVICE_ID);
104                }
105            }
106            if (!is_null($logger)) {
107                $service->setLogger($logger);
108            }
109        }
110
111        return $service;
112    }
113}