1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MaglMarkdown\View\Helper; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Interop\Container\Exception\ContainerException; |
7
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
8
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
9
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
10
|
|
|
use Zend\ServiceManager\FactoryInterface; |
11
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class MarkdownFactory |
15
|
|
|
* |
16
|
|
|
* @package MaglMarkdown\View\Helper |
17
|
|
|
*/ |
18
|
|
|
class MarkdownFactory implements FactoryInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create service |
23
|
|
|
* |
24
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
25
|
|
|
* @return Markdown |
26
|
|
|
*/ |
27
|
1 |
|
public function createService(ServiceLocatorInterface $serviceLocator) |
28
|
|
|
{ |
29
|
1 |
|
if ($serviceLocator instanceof AbstractPluginManager) { |
30
|
1 |
|
$serviceLocator = $serviceLocator->getServiceLocator() ?: $serviceLocator; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
1 |
|
return $this->create($serviceLocator); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create an object |
38
|
|
|
* |
39
|
|
|
* @param ContainerInterface $container |
40
|
|
|
* @param string $requestedName |
41
|
|
|
* @param null|array $options |
42
|
|
|
* @return Markdown |
43
|
|
|
* @throws \Interop\Container\Exception\NotFoundException |
44
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
45
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
46
|
|
|
* creating a service. |
47
|
|
|
* @throws ContainerException if any other error occurs |
48
|
|
|
*/ |
49
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
50
|
|
|
{ |
51
|
|
|
return $this->create($container); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param ServiceLocatorInterface|ContainerInterface $container |
56
|
|
|
* @return Markdown |
57
|
|
|
*/ |
58
|
1 |
View Code Duplication |
protected function create($container) |
|
|
|
|
59
|
|
|
{ |
60
|
1 |
|
if (!$container instanceof ServiceLocatorInterface && !$container instanceof ContainerInterface) { |
61
|
|
|
throw new \InvalidArgumentException('Invalid container to create service'); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return new Markdown($container->get('MaglMarkdown\MarkdownService')); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.