Completed
Push — master ( 030796...0c67b5 )
by Asmir
30:13
created

SoapContainerBuilder   B

Complexity

Total Complexity 15

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 17
dl 0
loc 146
rs 7.8571
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setContainerClassName() 0 7 1
B getContainerBuilder() 0 24 1
A fetchMetadata() 0 12 2
A getDebugContainer() 0 4 1
A getProdContainer() 0 5 1
A dumpContainerForProd() 0 10 2
A dump() 0 15 2
A createSerializerBuilderFromContainer() 0 5 1
A createSerializerBuilder() 0 18 3
1
<?php
2
namespace GoetasWebservices\SoapServices\SoapClient\Builder;
3
4
use GoetasWebservices\SoapServices\SoapClient\DependencyInjection\Compiler\CleanupPass;
5
use GoetasWebservices\SoapServices\SoapClient\DependencyInjection\Compiler\ConfigurePass;
6
use GoetasWebservices\SoapServices\SoapClient\DependencyInjection\SoapClientExtension;
7
use GoetasWebservices\WsdlToPhp\DependencyInjection\Wsdl2PhpExtension;
8
use GoetasWebservices\Xsd\XsdToPhp\DependencyInjection\Xsd2PhpExtension;
9
use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\BaseTypesHandler;
10
use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler;
11
use JMS\Serializer\Handler\HandlerRegistryInterface;
12
use JMS\Serializer\SerializerBuilder;
13
use Symfony\Component\Config\FileLocator;
14
use Symfony\Component\Config\Loader\DelegatingLoader;
15
use Symfony\Component\Config\Loader\LoaderResolver;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
19
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
21
22
class SoapContainerBuilder
23
{
24
    private $className = 'SoapClientContainer';
25
    private $classNs = 'SoapServicesStub';
26
27
    protected $configFile;
28
29
    public function __construct($configFile)
30
    {
31
        $this->configFile = $configFile;
32
    }
33
34
    public function setContainerClassName($fqcn)
35
    {
36
        $fqcn = strtr($fqcn, ['.' => '\\', '/' => '\\',]);
37
        $pos = strrpos($fqcn, '\\');
38
        $this->className = substr($fqcn, $pos + 1);
39
        $this->classNs = substr($fqcn, 0, $pos);
40
    }
41
42
    /**
43
     * @param array $metadata
44
     * @return ContainerBuilder
45
     */
46
    protected function getContainerBuilder(array $metadata = array())
47
    {
48
        $container = new ContainerBuilder();
49
50
        $container->registerExtension(new Wsdl2PhpExtension());
51
        $container->registerExtension(new Xsd2PhpExtension());
52
        $container->registerExtension(new SoapClientExtension());
53
        $container->addCompilerPass(new ConfigurePass());
54
        $container->addCompilerPass(new CleanupPass());
55
56
        $locator = new FileLocator('.');
57
        $loaders = array(
58
            new YamlFileLoader($container, $locator),
59
            new XmlFileLoader($container, $locator)
60
        );
61
        $delegatingLoader = new DelegatingLoader(new LoaderResolver($loaders));
62
        $delegatingLoader->load($this->configFile);
63
64
        $container->setParameter('goetas.soap_client.metadata', $metadata);
65
66
        $container->compile();
67
68
        return $container;
69
    }
70
71
    /**
72
     * @param ContainerInterface $debugContainer
73
     * @return array
74
     */
75
    protected function fetchMetadata(ContainerInterface $debugContainer)
76
    {
77
        $metadataReader = $debugContainer->get('goetas.wsdl2php.metadata_loader.dev');
78
        $wsdlMetadata = $debugContainer->getParameter('wsdl2php.config')['metadata'];
79
80
        $metadata = [];
81
        foreach (array_keys($wsdlMetadata) as $uri) {
82
            $metadata[$uri] = $metadataReader->load($uri);
83
        }
84
85
        return $metadata;
86
    }
87
88
    public function getDebugContainer()
89
    {
90
        return $this->getContainerBuilder();
91
    }
92
93
    /**
94
     * @return ContainerInterface
95
     */
96
    public function getProdContainer()
97
    {
98
        $ref = new \ReflectionClass("{$this->classNs}\\{$this->className}");
99
        return $ref->newInstance();
100
    }
101
102
    /**
103
     * @param $dir
104
     * @param ContainerInterface $debugContainer
105
     */
106
    public function dumpContainerForProd($dir, ContainerInterface $debugContainer)
107
    {
108
        $metadata = $this->fetchMetadata($debugContainer);
109
110
        if (!$metadata) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $metadata of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
111
            throw new \Exception("Empty metadata can not be used for production");
112
        }
113
        $forProdContainer = $this->getContainerBuilder($metadata);
114
        $this->dump($forProdContainer, $dir);
115
    }
116
117
    private function dump(ContainerBuilder $container, $dir)
118
    {
119
        $dumper = new PhpDumper($container);
120
        $options = [
121
            'debug' => false,
122
            'class' => $this->className,
123
            'namespace' => $this->classNs
124
        ];
125
126
        if (!is_dir($dir)) {
127
            mkdir($dir, 0777, true);
128
        }
129
130
        file_put_contents($dir . '/' . $this->className . '.php', $dumper->dump($options));
131
    }
132
133
    /**
134
     * @param ContainerInterface $container
135
     * @param callable $callback
136
     * @return SerializerBuilder
137
     */
138
    public static function createSerializerBuilderFromContainer(ContainerInterface $container, callable $callback = null)
139
    {
140
        $destinations = $container->getParameter('xsd2php.config')['destinations_jms'];
141
        return self::createSerializerBuilder($destinations, $callback);
142
    }
143
144
    /**
145
     * @param array $jmsMetadata
146
     * @param callable $callback
147
     * @return SerializerBuilder
148
     */
149
    public static function  createSerializerBuilder(array $jmsMetadata, callable $callback = null)
150
    {
151
        $serializerBuilder = SerializerBuilder::create();
152
        $serializerBuilder->configureHandlers(function (HandlerRegistryInterface $handler) use ($callback, $serializerBuilder) {
153
            $serializerBuilder->addDefaultHandlers();
154
            $handler->registerSubscribingHandler(new BaseTypesHandler()); // XMLSchema List handling
155
            $handler->registerSubscribingHandler(new XmlSchemaDateHandler()); // XMLSchema date handling
156
            if ($callback) {
157
                call_user_func($callback, $handler);
158
            }
159
        });
160
161
162
        foreach ($jmsMetadata as $php => $dir) {
163
            $serializerBuilder->addMetadataDir($dir, $php);
164
        }
165
        return $serializerBuilder;
166
    }
167
}
168