Directory::insertByAna()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 9.568
c 0
b 0
f 0
cc 4
nc 2
nop 2
crap 4
1
<?php
2
/******************************************************************************
3
 * An iterator interface over the Leagues flysystem.
4
 * Copyright (c) 2021, 2015 Richard Klees <[email protected]>
5
 *
6
 * This software is licensed under GPLv3. You should have received
7
 * a copy of the along with the code.
8
 */
9
10
namespace Lechimp\Flightcontrol;
11
12
/**
13
 * This class represents a directory in the FS.
14
 */
15
class Directory extends FixedFDirectory
16
{
17
    /**
18
     * Get the objects inside the directory.
19
     *
20
     * @return FSObject[]
21
     */
22 48
    public function contents() : array
23
    {
24 48
        $contents = $this->filesystem()->listContents($this->path());
25 48
        $returns = array();
26 48
        foreach ($contents as $content) {
27 43
            $returns[] = $this->flightcontrol->get($content["path"]);
28
        }
29 48
        return $returns;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 4
    public function mimetype() : ?string
36
    {
37 4
        return "directory";
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function toDirectory()
44
    {
45
        return $this;
46
    }
47
48
    /**
49
     * See documentation of FDirectory.
50
     *
51
     * @return FDirectory<File>
0 ignored issues
show
Documentation introduced by
The doc-type FDirectory<File> could not be parsed: Expected "|" or "end of type", but got "<" at position 10. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
52
     */
53 40
    public function unfix() : FDirectory
54
    {
55 40
        return $this->flightcontrol()->newFDirectory($this, function () {
56 40
            return $this->contents();
57 40
        });
58
    }
59
60
    /**
61
     * Create a directory structure with an anamorphism an insert it in place of
62
     * this directory.
63
     *
64
     * This is for INTERNAL use, use unfold($start_value)->with($unfolder) instead.
65
     *
66
     * @param   mixed     $start_value
67
     * @throws  \LogicException         When generated root node is a file.
68
     */
69 5
    public function insertByAna(\Closure $unfolder, $start_value) : void
70
    {
71 5
        $insert = FixedFDirectory::ana($unfolder, $start_value);
72
73 5
        if ($insert->isFile()) {
74 1
            throw new \LogicException("Expected generated root node to be a directory, not a file.");
75
        }
76
77 4
        $inserter = array();
78 4
        $inserter[0] = function ($path, FixedFDirectory $directory) use (&$inserter) {
79 4
            foreach ($directory->contents() as $content) {
80 4
                $new_path = $path . "/" . $content->name();
81 4
                if ($content->isFile()) {
82 4
                    $this->filesystem()->write($new_path, $content->content());
83
                } else {
84 1
                    $this->filesystem()->createDirectory($new_path);
85 1
                    $inserter[0]($new_path, $content);
86
                }
87
            }
88 4
        };
89 4
        $inserter[0]($this->path(), $insert);
90 4
    }
91
92
    /**
93
     * Make a directory when unfolding a directory structure via Directory::unfold.
94
     *
95
     * @param   Flightcontrol   $flightcontrol
96
     * @param   string          $name
97
     * @param   array           $content
98
     * @return  FDirectory<a>
0 ignored issues
show
Documentation introduced by
The doc-type FDirectory<a> could not be parsed: Expected "|" or "end of type", but got "<" at position 10. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
99
     */
100 5
    public static function makeFDirectory(Flightcontrol $flightcontrol, string $name, array $content) : FDirectory
101
    {
102 5
        return new FDirectory(
103 5
            new VirtualFSObject($flightcontrol, $name),
104 5
            function () use ($content) {
105 4
                return $content;
106 5
            }
107
        );
108
    }
109
110
    /**
111
     * Make a file when unfolding a directory structure via Directory::unfold.
112
     *
113
     * @param   Flightcontrol   $flightcontrol
114
     * @param   string  $name
115
     * @param   string  $content
116
     * @return  File
117
     */
118 5
    public static function makeFile(Flightcontrol $flightcontrol, string $name, string $content) : VirtualFile
119
    {
120 5
        return new VirtualFile($flightcontrol, $name, $content);
121
    }
122
}
123