We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 43 | 
| Total Lines | 295 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like BaseCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 39 | class BaseCommand extends Command  | 
            ||
| 40 | { | 
            ||
| 41 | /**  | 
            ||
| 42 | * @var CollectionRepository  | 
            ||
| 43 | */  | 
            ||
| 44 | protected $collectionRepository;  | 
            ||
| 45 | |||
| 46 | /**  | 
            ||
| 47 | * @var DocumentRepository  | 
            ||
| 48 | */  | 
            ||
| 49 | protected $documentRepository;  | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * @var LibraryRepository  | 
            ||
| 53 | */  | 
            ||
| 54 | protected $libraryRepository;  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * @var int  | 
            ||
| 58 | */  | 
            ||
| 59 | protected $storagePid;  | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * @var \Kitodo\Dlf\Domain\Model\Library  | 
            ||
| 63 | */  | 
            ||
| 64 | protected $owner;  | 
            ||
| 65 | |||
| 66 | /**  | 
            ||
| 67 | * Initialize the extbase repository based on the given storagePid.  | 
            ||
| 68 | *  | 
            ||
| 69 | * TYPO3 10+: Find a better solution e.g. based on Symfonie Dependancy Injection.  | 
            ||
| 70 | *  | 
            ||
| 71 | * @param int $storagePid The storage pid  | 
            ||
| 72 | *  | 
            ||
| 73 | * @return bool  | 
            ||
| 74 | */  | 
            ||
| 75 | protected function initializeRepositories($storagePid)  | 
            ||
| 76 |     { | 
            ||
| 77 |         if (MathUtility::canBeInterpretedAsInteger($storagePid)) { | 
            ||
| 78 | $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);  | 
            ||
| 79 | $frameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);  | 
            ||
| 80 | |||
| 81 | $frameworkConfiguration['persistence']['storagePid'] = MathUtility::forceIntegerInRange((int) $storagePid, 0);  | 
            ||
| 82 | $configurationManager->setConfiguration($frameworkConfiguration);  | 
            ||
| 83 | |||
| 84 | $this->collectionRepository = GeneralUtility::makeInstance(CollectionRepository::class);  | 
            ||
| 85 | $this->documentRepository = GeneralUtility::makeInstance(DocumentRepository::class);  | 
            ||
| 86 | $this->libraryRepository = GeneralUtility::makeInstance(LibraryRepository::class);  | 
            ||
| 87 |         } else { | 
            ||
| 88 | return false;  | 
            ||
| 89 | }  | 
            ||
| 90 | $this->storagePid = MathUtility::forceIntegerInRange((int) $storagePid, 0);  | 
            ||
| 91 | |||
| 92 | return true;  | 
            ||
| 93 | }  | 
            ||
| 94 | |||
| 95 | /**  | 
            ||
| 96 | * Return matching uid of Solr core depending on the input value.  | 
            ||
| 97 | *  | 
            ||
| 98 | * @param array $solrCores array of the valid Solr cores  | 
            ||
| 99 | * @param string|bool|null $inputSolrId possible uid or name of Solr core  | 
            ||
| 100 | *  | 
            ||
| 101 | * @return int matching uid of Solr core  | 
            ||
| 102 | */  | 
            ||
| 103 | protected function getSolrCoreUid(array $solrCores, $inputSolrId): int  | 
            ||
| 104 |     { | 
            ||
| 105 |         if (MathUtility::canBeInterpretedAsInteger($inputSolrId)) { | 
            ||
| 106 | $solrCoreUid = MathUtility::forceIntegerInRange((int) $inputSolrId, 0);  | 
            ||
| 107 |         } else { | 
            ||
| 108 | $solrCoreUid = $solrCores[$inputSolrId];  | 
            ||
| 109 | }  | 
            ||
| 110 | return $solrCoreUid;  | 
            ||
| 111 | }  | 
            ||
| 112 | |||
| 113 | /**  | 
            ||
| 114 | * Fetches all Solr cores on given page.  | 
            ||
| 115 | *  | 
            ||
| 116 | * @param int $pageId The UID of the Solr core or 0 to disable indexing  | 
            ||
| 117 | *  | 
            ||
| 118 | * @return array Array of valid Solr cores  | 
            ||
| 119 | */  | 
            ||
| 120 | protected function getSolrCores(int $pageId): array  | 
            ||
| 121 |     { | 
            ||
| 122 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)  | 
            ||
| 123 |             ->getQueryBuilderForTable('tx_dlf_solrcores'); | 
            ||
| 124 | |||
| 125 | $solrCores = [];  | 
            ||
| 126 | $result = $queryBuilder  | 
            ||
| 127 |             ->select('uid', 'index_name') | 
            ||
| 128 |             ->from('tx_dlf_solrcores') | 
            ||
| 129 | ->where(  | 
            ||
| 130 | $queryBuilder->expr()->eq(  | 
            ||
| 131 | 'pid',  | 
            ||
| 132 | $queryBuilder->createNamedParameter((int) $pageId, Connection::PARAM_INT)  | 
            ||
| 133 | )  | 
            ||
| 134 | )  | 
            ||
| 135 | ->execute();  | 
            ||
| 136 | |||
| 137 |         while ($record = $result->fetch()) { | 
            ||
| 138 | $solrCores[$record['index_name']] = $record['uid'];  | 
            ||
| 139 | }  | 
            ||
| 140 | |||
| 141 | return $solrCores;  | 
            ||
| 142 | }  | 
            ||
| 143 | |||
| 144 | /**  | 
            ||
| 145 | * Load XML file / IIIF resource from URL  | 
            ||
| 146 | *  | 
            ||
| 147 | * @access protected  | 
            ||
| 148 | *  | 
            ||
| 149 | * @param string $location: The URL of the file to load  | 
            ||
| 150 | *  | 
            ||
| 151 | * @return string|bool the found xml as string or false on failure  | 
            ||
| 152 | */  | 
            ||
| 153 | protected function loadLocation($location)  | 
            ||
| 154 |     { | 
            ||
| 155 | // Load XML / JSON-LD file.  | 
            ||
| 156 |         if (GeneralUtility::isValidUrl($location)) { | 
            ||
| 157 | // Load extension configuration  | 
            ||
| 158 | // $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey);  | 
            ||
| 159 | // // Set user-agent to identify self when fetching XML / JSON-LD data.  | 
            ||
| 160 |             // if (!empty($extConf['useragent'])) { | 
            ||
| 161 |             //     @ini_set('user_agent', $extConf['useragent']); | 
            ||
| 162 | // }  | 
            ||
| 163 | $fileResource = GeneralUtility::getUrl($location);  | 
            ||
| 164 |             if ($fileResource !== false) { | 
            ||
| 165 | $xml = Helper::getXmlFileAsString($fileResource);  | 
            ||
| 166 |                 if ($xml !== false) { | 
            ||
| 167 | return $xml;  | 
            ||
| 168 | }  | 
            ||
| 169 | }  | 
            ||
| 170 |         } else { | 
            ||
| 171 |             $this->logger->error('Invalid file location "' . $location . '" for document loading'); | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 172 | }  | 
            ||
| 173 | return false;  | 
            ||
| 174 | }  | 
            ||
| 175 | |||
| 176 | /**  | 
            ||
| 177 | * Update or insert document to database  | 
            ||
| 178 | *  | 
            ||
| 179 | * @param int|string $doc The document uid from DB OR the location of a mets document.  | 
            ||
| 180 | *  | 
            ||
| 181 | * @return bool true on success  | 
            ||
| 182 | */  | 
            ||
| 183 | protected function saveToDatabase(Document $document)  | 
            ||
| 286 | }  | 
            ||
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * Get the ID of the parent document if the current document has one.  | 
            ||
| 290 | * Currently only applies to METS documents.  | 
            ||
| 291 | *  | 
            ||
| 292 | * @access protected  | 
            ||
| 293 | *  | 
            ||
| 294 | * @return int The parent document's id.  | 
            ||
| 295 | */  | 
            ||
| 296 | protected function getParentDocumentUidForSaving(Document $document)  | 
            ||
| 334 | }  | 
            ||
| 335 | |||
| 336 | }  | 
            ||
| 337 |