FmLibServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 1/24/18
7
 * Time: 2:55 PM
8
 */
9
10
namespace Tfboe\FmLib\Providers;
11
12
use Doctrine\DBAL\Types\Type;
13
use Doctrine\ORM\EntityManagerInterface;
14
use Illuminate\Contracts\Container\Container;
15
use Illuminate\Contracts\Debug\ExceptionHandler;
16
use Illuminate\Support\Facades\Validator;
17
use Illuminate\Support\ServiceProvider;
18
use Irazasyed\JwtAuthGuard\JwtAuthGuardServiceProvider;
19
use LaravelDoctrine\Extensions\GedmoExtensionsServiceProvider;
20
use LaravelDoctrine\ORM\DoctrineServiceProvider;
21
use Tfboe\FmLib\Entity\Helpers\UTCDateTimeType;
22
use Tfboe\FmLib\Exceptions\Handler;
23
use Tfboe\FmLib\Http\Middleware\Authenticate;
24
use Tfboe\FmLib\Service\AsyncExecuterService;
25
use Tfboe\FmLib\Service\AsyncExecuterServiceInterface;
26
use Tfboe\FmLib\Service\DeletionService;
27
use Tfboe\FmLib\Service\DeletionServiceInterface;
28
use Tfboe\FmLib\Service\DynamicServiceLoadingService;
29
use Tfboe\FmLib\Service\DynamicServiceLoadingServiceInterface;
30
use Tfboe\FmLib\Service\LoadingService;
31
use Tfboe\FmLib\Service\LoadingServiceInterface;
32
use Tfboe\FmLib\Service\ObjectCreatorService;
33
use Tfboe\FmLib\Service\ObjectCreatorServiceInterface;
34
use Tfboe\FmLib\Service\PlayerService;
35
use Tfboe\FmLib\Service\PlayerServiceInterface;
36
use Tfboe\FmLib\Service\RankingSystem\EloRanking;
37
use Tfboe\FmLib\Service\RankingSystem\EloRankingInterface;
38
use Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier;
39
use Tfboe\FmLib\Service\RankingSystem\RecursiveEndStartTimeService;
40
use Tfboe\FmLib\Service\RankingSystemService;
41
use Tfboe\FmLib\Service\RankingSystemServiceInterface;
42
use Tymon\JWTAuth\Providers\LumenServiceProvider;
43
44
/**
45
 * Class FmLibServiceProvider
46
 * @package Tfboe\FmLib\Providers
47
 */
48
class FmLibServiceProvider extends ServiceProvider
49
{
50
//<editor-fold desc="Public Methods">
51
  /**
52
   * Bootstrap the application services.
53
   *
54
   * @return void
55
   */
56
  public function boot()
57
  {
58
    app()->configure('fm-lib');
59
60
    /** @noinspection PhpUndefinedMethodInspection */
61
    Validator::extend('IntegerType', function (/** @noinspection PhpUnusedParameterInspection */
62
      $attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters 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...
Unused Code introduced by
The parameter $validator 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...
63
      return is_int($value);
64
    }, 'The :attribute must be an integer.');
65
66
    include __DIR__ . '/../routes.php';
67
  }
68
69
  /** @noinspection PhpDocMissingThrowsInspection */ //\Doctrine\DBAL\DBALException
70
  /**
71
   * Register the application services.
72
   *
73
   * @return void
74
   */
75
  public function register()
76
  {
77
    //register middleware
78
    app()->routeMiddleware(['auth' => Authenticate::class]);
79
80
    $this->app->singleton(
81
      ExceptionHandler::class,
82
      Handler::class
83
    );
84
85
    if ($this->app->environment() !== 'production') {
86
      if (class_exists('\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider')) {
87
        $this->app->register('\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider');
88
      }
89
    }
90
91
    /** @noinspection PhpUnhandledExceptionInspection */ // \Doctrine\DBAL\DBALException datetime is a valid type
92
    Type::overrideType('datetime', UTCDateTimeType::class);
93
94
    $this->app->register(LumenServiceProvider::class);
95
    $this->app->register(DoctrineServiceProvider::class);
96
    $this->app->register(GedmoExtensionsServiceProvider::class);
97
    $this->app->register(JwtAuthGuardServiceProvider::class);
98
99
    $this->app->singleton(DynamicServiceLoadingServiceInterface::class, function (Container $app) {
100
      return new DynamicServiceLoadingService($app);
101
    });
102
103
    $this->app->singleton(RankingSystemServiceInterface::class, function (Container $app) {
104
      return new RankingSystemService($app->make(DynamicServiceLoadingServiceInterface::class),
105
        $app->make(EntityManagerInterface::class));
106
    });
107
108
    $this->app->singleton(ObjectCreatorServiceInterface::class, function () {
109
      return new ObjectCreatorService();
110
    });
111
112
    $this->app->singleton(EloRankingInterface::class, function (Container $app) {
113
      $timeService = new RecursiveEndStartTimeService();
114
      return new EloRanking(
115
        $app->make(EntityManagerInterface::class),
116
        $timeService,
117
        new EntityComparerByTimeStartTimeAndLocalIdentifier($timeService),
118
        $app->make(ObjectCreatorServiceInterface::class));
119
    });
120
121
    $this->app->singleton(LoadingServiceInterface::class, function (Container $app) {
122
      return new LoadingService($app->make(EntityManagerInterface::class));
123
    });
124
125
    $this->app->singleton(PlayerServiceInterface::class, function (Container $app) {
126
      return new PlayerService(
127
        $app->make(EntityManagerInterface::class),
128
        $app->make(LoadingServiceInterface::class),
129
        $app->make(RankingSystemServiceInterface::class)
130
      );
131
    });
132
133
    $this->app->singleton(AsyncExecuterServiceInterface::class, function () {
134
      return new AsyncExecuterService();
135
    });
136
  }
137
//</editor-fold desc="Public Methods">
138
}