FileInstaller::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
namespace Mediact\Composer;
3
4
use Composer\IO\IOInterface;
5
use Mediact\FileMapping\FileMappingInterface;
6
use Mediact\FileMapping\FileMappingReaderInterface;
7
use SplFileObject;
8
9
class FileInstaller
10
{
11
    /** @var FileMappingReaderInterface */
12
    private $mappingReader;
13
14
    /**
15
     * Constructor.
16
     *
17
     * @param FileMappingReaderInterface $mappingReader
18
     */
19
    public function __construct(FileMappingReaderInterface $mappingReader)
20
    {
21
        $this->mappingReader = $mappingReader;
22
    }
23
24
    /**
25
     * Install the deployer files.
26
     *
27
     * @param IOInterface $io
28
     *
29
     * @return void
30
     *
31
     * @SuppressWarnings(PHPMD.ShortVariable)
32
     */
33
    public function install(IOInterface $io)
34
    {
35
        foreach ($this->mappingReader as $mapping) {
36
            if (file_exists($mapping->getDestination())) {
37
                continue;
38
            }
39
40
            $this->installFile($mapping);
41
42
            $io->write(
43
                sprintf(
44
                    '<info>Installed:</info> %s',
45
                    $mapping->getRelativeDestination()
46
                )
47
            );
48
        }
49
    }
50
51
    /**
52
     * Install the given file if it does not exist.
53
     *
54
     * @param FileMappingInterface $mapping
55
     *
56
     * @return void
57
     *
58
     * @SuppressWarnings(PHPMD.ShortVariable)
59
     */
60
    public function installFile(FileMappingInterface $mapping)
61
    {
62
        $inputFile  = new SplFileObject($mapping->getSource(), 'r');
63
        $targetFile = new SplFileObject($mapping->getDestination(), 'w+');
64
65
        foreach ($inputFile as $input) {
66
            $targetFile->fwrite($input);
0 ignored issues
show
Bug introduced by
It seems like $input can also be of type array; however, parameter $data of SplFileObject::fwrite() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            $targetFile->fwrite(/** @scrutinizer ignore-type */ $input);
Loading history...
67
        }
68
    }
69
}
70