Passed
Push — 1.0 ( 550c2d...ebbabc )
by Morven
08:49
created

DownloadFolder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 13
dl 0
loc 39
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onBeforeWrite() 0 11 2
A requireDefaultRecords() 0 9 1
1
<?php
2
3
namespace SilverCommerce\DownloadableProducts;
4
5
use SilverStripe\ORM\DB;
6
use SilverStripe\Assets\Folder;
7
use SilverStripe\Security\InheritedPermissions;
8
9
/**
10
 * A custom type of folder used to 
11
 */
12
class DownloadFolder extends Folder
13
{
14
    /**
15
     * The location of this folder
16
     * 
17
     * @var string
18
     */
19
    private static $folder_name = "downloadableproducts";
20
21
    /**
22
     * Setup defaults when a dev build is run
23
     * 
24
     * @return void
25
     */
26
    public function requireDefaultRecords()
27
    {
28
        parent::requireDefaultRecords();
29
30
        // Setup, or change, the top level download folder.
31
        $download_folder = static::find_or_make(static::config()->folder_name);
32
        $download_folder->ClassName = self::class;
33
        $download_folder->write();
34
        DB::alteration_message("Setup downloadableproduct folder");
35
    }
36
37
    /**
38
     * Ensure this folder has correct permissions
39
     */
40
    public function onBeforeWrite()
41
    {
42
        parent::onBeforeWrite();
43
44
        $allowed = [
45
            InheritedPermissions::LOGGED_IN_USERS,
46
            InheritedPermissions::ONLY_THESE_USERS
47
        ];
48
49
        if (!in_array($this->CanViewType, $allowed)) {
50
            $this->CanViewType = InheritedPermissions::LOGGED_IN_USERS;
0 ignored issues
show
Bug Best Practice introduced by
The property CanViewType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
        }
52
    }
53
54
}