Completed
Push — master ( c4c649...533f13 )
by
unknown
12s
created

RegistryImportFeed::setAssetsDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\Registry;
4
5
use League\Flysystem\Plugin\ListFiles;
6
use SilverStripe\Assets\Storage\GeneratedAssetHandler;
7
use SilverStripe\Control\RSS\RSSFeed;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Core\Injector\Injectable;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\FieldType\DBDatetime;
13
14
class RegistryImportFeed
15
{
16
    use Configurable;
17
    use Injectable;
18
19
    /**
20
     * The path format to store imported record files in (inside the assets directory)
21
     *
22
     * @config
23
     * @var string
24
     */
25
    private static $storage_path = '_imports/{model}';
0 ignored issues
show
introduced by
The private property $storage_path is not used, and could be removed.
Loading history...
26
27
    /**
28
     * The filename to use for storing imported record files. Used by RegistryImportFeedController to save files to.
29
     *
30
     * @config
31
     * @var string
32
     */
33
    private static $storage_filename = 'import-{date}.csv';
0 ignored issues
show
introduced by
The private property $storage_filename is not used, and could be removed.
Loading history...
34
35
    protected $modelClass;
36
37
    /**
38
     * The class used to manipulate imported feed files on the filesystem
39
     *
40
     * @var GeneratedAssetHandler
41
     */
42
    protected $assetHandler;
43
44
    /**
45
     * The "assets" folder name
46
     *
47
     * @var string
48
     */
49
    protected $assetsDir;
50
51
    public function setModelClass($class)
52
    {
53
        $this->modelClass = $class;
54
        return $this;
55
    }
56
57
    public function getLatest()
58
    {
59
        $registryPage = RegistryPage::get()->filter(['DataClass' => $this->modelClass])->first();
60
        if ($registryPage && $registryPage->exists()) {
61
            $files = $this->getImportFiles();
62
        } else {
63
            // Always return an empty list of the model isn't associated to any RegistryPages
64
            $files = ArrayList::create();
65
        }
66
67
        return RSSFeed::create(
68
            $files,
0 ignored issues
show
Bug introduced by
It seems like $files can also be of type SilverStripe\ORM\ArrayList; however, parameter $args of SilverStripe\View\ViewableData::create() does only seem to accept array, 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

68
            /** @scrutinizer ignore-type */ $files,
Loading history...
69
            'registry-feed/latest/' . $this->sanitiseClassName($this->modelClass),
0 ignored issues
show
Bug introduced by
'registry-feed/latest/' ...Name($this->modelClass) of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

69
            /** @scrutinizer ignore-type */ 'registry-feed/latest/' . $this->sanitiseClassName($this->modelClass),
Loading history...
70
            singleton($this->modelClass)->singular_name() . ' data import history'
71
        );
72
    }
73
74
    /**
75
     * Set the handler used to manipulate the filesystem, and add the ListFiles plugin from Flysystem to inspect
76
     * the contents of a directory
77
     *
78
     * @param GeneratedAssetHandler $handler
79
     * @return $this
80
     */
81
    public function setAssetHandler(GeneratedAssetHandler $handler)
82
    {
83
        $handler->getFilesystem()->addPlugin(new ListFiles);
84
85
        $this->assetHandler = $handler;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Get the handler used to manipulate the filesystem
92
     *
93
     * @return GeneratedAssetHandler
94
     */
95
    public function getAssetHandler()
96
    {
97
        return $this->assetHandler;
98
    }
99
100
    /**
101
     * Get the path that import files will be stored for this model
102
     *
103
     * @param string $modelClass If null, the current model class will be used
104
     * @return string
105
     */
106
    public function getStoragePath($modelClass = null)
107
    {
108
        $sanitisedClassName = $this->sanitiseClassName($modelClass ?: $this->modelClass);
109
        return str_replace('{model}', $sanitisedClassName, $this->config()->get('storage_path'));
110
    }
111
112
    /**
113
     * Loop import files in the storage path and push them into an {@link ArrayList}
114
     *
115
     * @return ArrayList
116
     */
117
    public function getImportFiles()
118
    {
119
        $path = $this->getStoragePath();
120
        $importFiles = $this->getAssetHandler()->getFilesystem()->listFiles($path);
121
122
        $files = ArrayList::create();
123
124
        foreach ($importFiles as $importFile) {
125
            $files->push(RegistryImportFeedEntry::create(
126
                $importFile['basename'],
127
                '',
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

127
                /** @scrutinizer ignore-type */ '',
Loading history...
128
                DBDatetime::create()->setValue($importFile['timestamp'])->Format(DBDatetime::ISO_DATETIME),
129
                $this->getAssetsDir() . '/' . $importFile['path']
130
            ));
131
        }
132
133
        return $files;
134
    }
135
136
    /**
137
     * Returns a relatively unique filename to storage imported data feeds as
138
     *
139
     * @return string
140
     */
141
    public function getImportFilename()
142
    {
143
        // Note: CLDR date format see DBDatetime
144
        $datetime = DBDatetime::now()->Format('y-MM-dd-HHmmss');
145
        return str_replace('{date}', $datetime, $this->config()->get('storage_filename'));
146
    }
147
148
    /**
149
     * Set the assets directory name
150
     *
151
     * @param string $assetsDir
152
     * @return $this
153
     */
154
    public function setAssetsDir($assetsDir)
155
    {
156
        $this->assetsDir = $assetsDir;
157
        return $this;
158
    }
159
160
    /**
161
     * Get the assets directory name
162
     *
163
     * @return string
164
     */
165
    public function getAssetsDir()
166
    {
167
        return $this->assetsDir;
168
    }
169
170
    /**
171
     * See {@link \SilverStripe\Admin\ModelAdmin::sanitiseClassName}
172
     *
173
     * @param  string $class
174
     * @return string
175
     */
176
    protected function sanitiseClassName($class)
177
    {
178
        return str_replace('\\', '-', $class);
179
    }
180
}
181