Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
DuplicateFileRevisionChecker
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 findDuplicates
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 removeIgnoredFiles
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace FileImporter\Services;
4
5use File;
6use FileImporter\Data\FileRevision;
7use FileRepo;
8
9/**
10 * Class that can be used to check if a FileRevision already exists on the current wiki.
11 * Only current / latest and non deleted files are checked.
12 *
13 * @license GPL-2.0-or-later
14 * @author Addshore
15 */
16class DuplicateFileRevisionChecker {
17
18    private FileRepo $fileRepo;
19
20    public function __construct( FileRepo $fileRepo ) {
21        $this->fileRepo = $fileRepo;
22    }
23
24    /**
25     * @return File[] array of matched files
26     */
27    public function findDuplicates( FileRevision $fileRevision ) {
28        $sha1 = $fileRevision->getField( 'sha1' );
29        if ( !$sha1 ) {
30            return [];
31        }
32
33        $files = $this->fileRepo->findBySha1( $sha1 );
34        $files = $this->removeIgnoredFiles( $files );
35
36        return $files;
37    }
38
39    /**
40     * This removed removes files that are either old or deleted.
41     *
42     * @param File[] $files
43     *
44     * @return File[]
45     */
46    private function removeIgnoredFiles( array $files ) {
47        $wantedFiles = [];
48
49        foreach ( $files as $file ) {
50            if (
51                $file->isOld() ||
52                $file->isDeleted( File::DELETED_FILE )
53            ) {
54                continue;
55            }
56            $wantedFiles[] = $file;
57        }
58
59        return $wantedFiles;
60    }
61
62}