Passed
Push — develop ( d4fe34...f94e96 )
by Lorenzo
02:08 queued 13s
created

src/core/decorators/InjectBean.ts   A

Complexity

Total Complexity 4
Complexity/F 2

Size

Lines of Code 24
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 13
mnd 2
bc 2
fnc 2
dl 0
loc 24
bpm 1
cpm 2
noi 0
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10

2 Functions

Rating   Name   Duplication   Size   Complexity  
A InjectBean.ts ➔ InjectBean 0 10 1
A InjectBean.ts ➔ getSingleton 0 9 3
1
import { logger } from '@/core';
2
3
function getSingleton(singletonClass: any) {
4 9
  if (singletonClass) {
5 8
    if (singletonClass.instance) {
6 6
      return singletonClass.instance;
7
    }
8 2
    throw new Error(`Cannot get instance from ${singletonClass.name}. Make sure that ${singletonClass.name} has @Bean as class decorator`);
9
  }
10 1
  throw new Error('Please specify the type of Bean. Example: @InjectBean(BeanClass)');
11
}
12
13
/**
14
 * Gets singleton instance by its static property
15
 * @decorator
16
 * @param singletonClass
17
 */
18
export function InjectBean<T>(singletonClass: T) {
19 6
  return (_value: any, context: ClassFieldDecoratorContext) => () => {
20 6
    logger.debug(`initializing ${String(context.name)} with instance of bean ${getSingleton(singletonClass).className}`);
21 3
    return getSingleton(singletonClass);
22
  };
23
}
24