Completed
Push — master ( 522461...c89cc2 )
by Fabien
02:24
created

FileReferenceService::getDataService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Fab\Media\Resource;
4
5
/*
6
 * This file is part of the Fab/Media project under GPLv2 or later.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.md file that was distributed with this source code.
10
 */
11
12
use Fab\Vidi\Service\DataService;
13
use TYPO3\CMS\Core\Database\ConnectionPool;
14
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
15
use TYPO3\CMS\Core\Resource\File;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18
/**
19
 * File Reference Service.
20
 */
21
class FileReferenceService
22
{
23
    /**
24
     * Return all references found in sys_file_reference.
25
     *
26
     * @param File|int $file
27
     * @return array
28
     */
29 View Code Duplication
    public function findFileReferences($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
32
        $fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file;
0 ignored issues
show
Bug introduced by
The class TYPO3\CMS\Core\Resource\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
33
34
        // Get the file references of the file.
35
        return $this->getDataService()->getRecords(
36
            'sys_file_reference',
37
            [
38
                'uid_local' => $fileIdentifier,
39
            ]
40
        );
41
    }
42
43
    /**
44
     * Return soft image references.
45
     *
46
     * @param File|int $file
47
     * @return array
48
     */
49 View Code Duplication
    public function findSoftImageReferences($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
52
        $fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file;
0 ignored issues
show
Bug introduced by
The class TYPO3\CMS\Core\Resource\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
53
54
        // Get the file references of the file in the RTE.
55
        $softReferences = $this->getDataService()->getRecords(
56
            'sys_refindex',
57
            [
58
                'softref_key' => 'rtehtmlarea_images',
59
                'ref_table' => 'sys_file',
60
                'ref_uid' => $fileIdentifier,
61
            ]
62
        );
63
        return $softReferences;
64
    }
65
66
    /**
67
     * Return link image references.
68
     *
69
     * @param File|int $file
70
     * @return array
71
     */
72 View Code Duplication
    public function findSoftLinkReferences($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
75
        $fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file;
0 ignored issues
show
Bug introduced by
The class TYPO3\CMS\Core\Resource\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
76
77
        // Get the link references of the file.
78
        $softReferences = $this->getDataService()->getRecords(
79
            'sys_refindex',
80
            [
81
                'softref_key' => 'typolink_tag',
82
                'ref_table' => 'sys_file',
83
                'ref_uid' => $fileIdentifier,
84
            ]
85
        );
86
        return $softReferences;
87
    }
88
89
    /**
90
     * Count all references found in sys_file_reference.
91
     *
92
     * @param File|int $file
93
     * @return int
94
     */
95 View Code Duplication
    public function countFileReferences($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file;
0 ignored issues
show
Bug introduced by
The class TYPO3\CMS\Core\Resource\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
98
99
        return $this->getDataService()
100
            ->count(
101
            'sys_file_reference',
102
            [
103
                'uid_local' => $fileIdentifier
104
            ]
105
        );
106
    }
107
108
    /**
109
     * Count soft image references.
110
     *
111
     * @param File|int $file
112
     * @return int
113
     */
114 View Code Duplication
    public function countSoftImageReferences($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        $fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file;
0 ignored issues
show
Bug introduced by
The class TYPO3\CMS\Core\Resource\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
117
118
        return $this->getDataService()
119
            ->count(
120
                'sys_refindex',
121
                [
122
                    'softref_key' => 'rtehtmlarea_images',
123
                    'ref_table' => 'sys_file',
124
                    'ref_uid' => $fileIdentifier
125
                ]
126
            );
127
    }
128
129
    /**
130
     * Count link image references.
131
     *
132
     * @param File|int $file
133
     * @return int
134
     */
135 View Code Duplication
    public function countSoftLinkReferences($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $fileIdentifier = $file instanceof File ? $file->getUid() : (int)$file;
0 ignored issues
show
Bug introduced by
The class TYPO3\CMS\Core\Resource\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
138
139
        return $this->getDataService()
140
            ->count(
141
                'sys_refindex',
142
                [
143
                    'softref_key' => 'typolink_tag',
144
                    'ref_table' => 'sys_file',
145
                    'ref_uid' => $fileIdentifier
146
                ]
147
            );
148
    }
149
150
    /**
151
     * Count total reference.
152
     *
153
     * @param File|int $file
154
     * @return int
155
     */
156
    public function countTotalReferences($file)
157
    {
158
        $numberOfReferences = $this->countFileReferences($file);
159
        $numberOfReferences += $this->countSoftImageReferences($file);
160
        $numberOfReferences += $this->countSoftLinkReferences($file);
161
162
        return $numberOfReferences;
163
    }
164
165
    /**
166
     * @param string $tableName
167
     * @return object|QueryBuilder
168
     */
169
    protected function getQueryBuilder($tableName): QueryBuilder
170
    {
171
        /** @var ConnectionPool $connectionPool */
172
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
173
        return $connectionPool->getQueryBuilderForTable($tableName);
174
    }
175
176
    /**
177
     * @return object|DataService
178
     */
179
    protected function getDataService(): DataService
180
    {
181
        return GeneralUtility::makeInstance(DataService::class);
182
    }
183
}
184