Passed
Push — trunk ( a2366d...897fa6 )
by Christian
16:38 queued 13s
created

repository.init.ts ➔ initializeRepositoryFactory   B

Complexity

Conditions 5

Size

Total Lines 55
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 44
c 0
b 0
f 0
dl 0
loc 55
rs 8.3573

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
/**
2
 * @package admin
3
 */
4
5
const RepositoryFactory = Shopware.Classes._private.RepositoryFactory;
6
const { EntityHydrator, ChangesetGenerator, EntityFactory } = Shopware.Data;
7
const ErrorResolverError = Shopware.Data.ErrorResolver;
8
9
const customEntityTypes = [{
10
    name: 'custom_entity_detail',
11
    icon: 'regular-image-text',
12
    // ToDo NEXT-22655 - Re-implement, when custom_entity_list page is available
13
    // }, {
14
    //     name: 'custom_entity_list',
15
    //     icon: 'regular-list',
16
}];
17
18
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
19
export default function initializeRepositoryFactory(container: InitContainer) {
20
    const httpClient = container.httpClient;
21
    const factoryContainer = Shopware.Application.getContainer('factory');
22
    const serviceContainer = Shopware.Application.getContainer('service');
23
24
    return httpClient.get('_info/entity-schema.json', {
25
        headers: {
26
            Authorization: `Bearer ${serviceContainer.loginService.getToken()}`,
27
        },
28
    }).then(({ data }) => {
29
        const entityDefinitionFactory = factoryContainer.entityDefinition;
30
        const customEntityDefinitionService = serviceContainer.customEntityDefinitionService;
31
        const cmsPageTypeService = serviceContainer.cmsPageTypeService;
32
        let hasCmsAwareDefinitions = false;
33
34
        // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
35
        Object.entries(data).forEach(([key, value]) => {
36
            entityDefinitionFactory.add(key, value);
37
38
            if (key.startsWith('custom_entity_') || key.startsWith('ce_')) {
39
                // @ts-expect-error - value is defined
40
                customEntityDefinitionService.addDefinition(value);
41
                // @ts-expect-error - value is defined
42
                // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
43
                hasCmsAwareDefinitions = hasCmsAwareDefinitions || !!value?.flags?.['cms-aware'];
44
            }
45
        });
46
47
        if (hasCmsAwareDefinitions) {
48
            customEntityTypes.forEach((customEntityType) => {
49
                cmsPageTypeService.register(customEntityType);
50
            });
51
        }
52
53
        const hydrator = new EntityHydrator();
54
        const changesetGenerator = new ChangesetGenerator();
55
        const entityFactory = new EntityFactory();
56
        const errorResolver = new ErrorResolverError();
57
58
        Shopware.Application.addServiceProvider('repositoryFactory', () => {
59
            return new RepositoryFactory(
60
                hydrator,
61
                changesetGenerator,
62
                entityFactory,
63
                httpClient,
64
                errorResolver,
65
            );
66
        });
67
        Shopware.Application.addServiceProvider('entityHydrator', () => {
68
            return hydrator;
69
        });
70
        Shopware.Application.addServiceProvider('entityFactory', () => {
71
            return entityFactory;
72
        });
73
    });
74
}
75