SatisServiceProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 17
dl 0
loc 114
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 5 1
B register() 0 98 6
1
<?php
2
3
namespace App\Providers;
4
5
use App\Satis\Collections\PackageCollection;
6
use App\Satis\Collections\RepositoryCollection;
7
use App\Satis\ConfigBuilder;
8
use App\Satis\ConfigManager;
9
use App\Satis\ConfigMirror;
10
use App\Satis\ConfigPersister;
11
use App\Satis\Model\ConfigLock;
12
use App\Satis\Model\Package;
13
use Illuminate\Filesystem\Filesystem;
14
use Illuminate\Support\Collection;
15
use Illuminate\Support\ServiceProvider;
16
use JMS\Serializer\Context;
17
use JMS\Serializer\Handler\HandlerRegistry;
18
use JMS\Serializer\SerializerBuilder;
19
use Doctrine\Common\Annotations\AnnotationRegistry;
20
use JMS\Serializer\VisitorInterface;
21
22
/**
23
 * @author Lukas Homza <[email protected]>
24
 */
25
class SatisServiceProvider extends ServiceProvider {
26
	/**
27
     * @return void
28
     */
29
    public function boot() {
30
        AnnotationRegistry::registerAutoloadNamespace(
31
            'JMS\Serializer\Annotation', base_path('vendor/jms/serializer/src')
32
        );
33
    }
34
35
    /**
36
     * Register any application services.
37
     *
38
     * @return void
39
     */
40
    public function register() {
41
        $serializer = SerializerBuilder::create()
42
            ->configureHandlers(function(HandlerRegistry $registry) {
43
                $registry->registerHandler(
44
                    'serialization',
45
                    'App\Satis\Collections\RepositoryCollection',
46
                    'json',
47
                    function(VisitorInterface $visitor, Collection $collection, array $type, Context $context) {
48
                        return $visitor->visitArray($collection->values(), $type, $context);
49
                    }
50
                );
51
52
                $registry->registerHandler(
53
                    'serialization',
54
                    'App\Satis\Collections\PackageCollection',
55
                    'json',
56
                    function(VisitorInterface $visitor, Collection $collection, array $type, Context $context) {
57
                        $output = [];
58
                        foreach($collection->values() as $package) {
59
                            /** @var Package $package */
60
61
                            $output[$package->getName()] = $package->getVersion();
62
                        }
63
64
                        if(!$output) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $output 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...
65
                            return null;
66
                        }
67
68
                        $type = array('name' => 'array');
69
70
                        return $visitor->visitArray($output, $type, $context);
71
                    }
72
                );
73
            })
74
            ->configureHandlers(function(HandlerRegistry $registry) {
75
                $registry->registerHandler(
76
                    'deserialization',
77
                    'App\Satis\Collections\RepositoryCollection',
78
                    'json',
79
                    function(VisitorInterface $visitor, array $data, array $type, Context $context) {
80
                        /** @var \App\Satis\Model\Repository[] $repositories */
81
                        $repositories = $visitor->visitArray($data, $type, $context);
82
83
                        $collection = new RepositoryCollection();
84
                        foreach($repositories as $repository) {
85
                            $collection->put($repository->getId(), $repository);
86
                        }
87
88
                        return $collection;
89
                    }
90
                );
91
92
                $registry->registerHandler(
93
                    'deserialization',
94
                    'App\Satis\Collections\PackageCollection',
95
                    'json',
96
                    function(VisitorInterface $visitor, array $data, array $type, Context $context) {
97
                        $temp = [];
98
                        foreach($data as $name => $version) {
99
                            $temp[] = ['name' => $name, 'version' => $version];
100
                        }
101
102
                        /** @var \App\Satis\Model\Package[] $packages */
103
                        $packages = $visitor->visitArray($temp, $type, $context);
104
105
                        $collection = new PackageCollection();
106
                        foreach($packages as $package) {
107
                            $collection->put($package->getId(), $package);
108
                        }
109
110
                        return $collection;
111
                    }
112
                );
113
            })
114
            ->build();
115
116
        $this->app->bind('App\Satis\ConfigMirror', function($app) use($serializer) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
            return new ConfigMirror($serializer);
118
        });
119
120
        $this->app->bind('App\Satis\ConfigManager', function($app) use($serializer) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
            $configPersister = $this->app->make('App\Satis\ConfigPersister');
122
123
            return new ConfigManager($configPersister, new ConfigBuilder($configPersister), $serializer);
124
        });
125
126
        $this->app->bind('App\Satis\ConfigPersister', function($app) use($serializer) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
            $filesystem = new Filesystem();
128
            $configLock = new ConfigLock();
129
            $configMirror = $this->app->make('App\Satis\ConfigMirror');
130
131
            return new ConfigPersister($filesystem, $configLock, $configMirror, $serializer);
132
        });
133
134
        $this->app->bind('JMS\Serializer\Serializer', function($app) use($serializer) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
            return $serializer;
136
        });
137
    }
138
}
139