Passed
Push — master ( 0e7a3d...2c4b2e )
by Sergey
02:01
created

RBAcModule.forDynamic   C

Complexity

Conditions 9

Size

Total Lines 60
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 60
rs 6.4339
c 0
b 0
f 0
cc 9

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { DynamicModule, Global, Module } from '@nestjs/common';
2
import { RbacService } from './services/rbac.service';
3
import { ModuleRef, Reflector } from '@nestjs/core';
4
import { StorageRbacService } from './services/storage.rbac.service';
5
import { IStorageRbac } from './interfaces/storage.rbac.interface';
6
import { IDynamicStorageRbac } from './interfaces/dynamic.storage.rbac.interface';
7
import { ICacheRBAC } from './interfaces/cache.rbac.interface';
8
9
@Global()
10
@Module({
11
  providers: [
12
    RbacService,
13
    StorageRbacService,
14
    Reflector,
15
  ],
16
  imports: [],
17
  exports: [
18
    RbacService,
19
  ],
20
})
21
export class RBAcModule {
22
  private static cache?: any | ICacheRBAC;
23
  private static cacheOptions?: { KEY?: string, TTL?: number };
24
25
  static useCache(
26
    cache: any | ICacheRBAC,
27
    options?: {
28
      KEY?: string,
29
      TTL?: number
30
    },
31
  ) {
32
    RBAcModule.cache = cache;
33
    RBAcModule.cacheOptions = options;
34
    return RBAcModule;
35
  }
36
37
  static forRoot(
38
    rbac: IStorageRbac,
39
    providers?: any[],
40
    imports?: any[],
41
  ): DynamicModule {
42
43
    return RBAcModule.forDynamic(
44
      /* tslint:disable */
45
      class {
46
        async getRbac(): Promise<IStorageRbac> {
47
          return rbac;
48
        };
49
      },
50
      providers,
51
      imports,
52
    );
53
  }
54
55
  static forDynamic(
56
    rbac: new () => IDynamicStorageRbac,
57
    providers?: any[],
58
    imports?: any[],
59
  ): DynamicModule {
60
    const inject = [ModuleRef, rbac];
61
62
    if (RBAcModule.cache) {
63
      inject.push(RBAcModule.cache);
64
    }
65
66
    const commonProviders = [
67
      ...(providers || []),
68
      rbac,
69
      {
70
        provide: StorageRbacService,
71
        useFactory: async (moduleRef: ModuleRef, rbacService: IDynamicStorageRbac, cache?: ICacheRBAC) => {
72
          if (cache && !RBAcModule.cacheOptions) {
73
            return cache;
74
          }
75
          if (cache && RBAcModule.cacheOptions.KEY) {
76
            cache.KEY = RBAcModule.cacheOptions.KEY;
77
          }
78
79
          if (cache && RBAcModule.cacheOptions.TTL) {
80
            cache.TTL = RBAcModule.cacheOptions.TTL;
81
          }
82
          return new StorageRbacService(moduleRef, rbacService, cache);
83
        },
84
        inject,
85
      },
86
    ];
87
88
    if (RBAcModule.cache) {
89
      commonProviders.push(RBAcModule.cache, {
90
        provide: 'ICacheRBAC',
91
        useFactory: (cache: ICacheRBAC): ICacheRBAC => {
92
          if (!RBAcModule.cacheOptions) {
93
            return cache;
94
          }
95
          if (RBAcModule.cacheOptions.KEY) {
96
            cache.KEY = RBAcModule.cacheOptions.KEY;
97
          }
98
99
          if (RBAcModule.cacheOptions.TTL) {
100
            cache.TTL = RBAcModule.cacheOptions.TTL;
101
          }
102
103
          return cache;
104
        },
105
        inject: [RBAcModule.cache],
106
      });
107
    }
108
109
    return {
110
      module: RBAcModule,
111
      providers: commonProviders,
112
      imports: [
113
        ...(imports || []),
114
      ],
115
    };
116
  }
117
}
118