DownloadableProduct   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
eloc 65
dl 0
loc 175
c 2
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 16 1
A getDownloadLink() 0 17 2
A getDownloadFolder() 0 4 1
A canDownload() 0 27 4
A onBeforeWrite() 0 26 6
A getDeliverable() 0 3 1
1
<?php
2
3
namespace SilverCommerce\DownloadableProducts;
4
5
use Product;
0 ignored issues
show
Bug introduced by
The type Product was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Assets\File;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\Security\Security;
9
use SilverStripe\Security\Member;
10
use SilverStripe\Core\Config\Config;
11
use SilverCommerce\OrdersAdmin\Model\Invoice;
12
use SilverStripe\AssetAdmin\Forms\UploadField;
13
use SilverCommerce\DownloadableProducts\DownloadFolder;
14
use SilverCommerce\DownloadableProducts\FileDownloadController;
15
use SilverStripe\Core\Injector\Injector;
16
17
/**
18
 * Product class that will allow adding of product to the CMS.
19
 */
20
class DownloadableProduct extends Product
21
{
22
23
    /**
24
     * Set the default DB table name
25
     * 
26
     * @var string
27
     */
28
    private static $table_name = "DownloadableProduct";
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
29
30
    private static $singular_name = 'Downloadable Product';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
31
32
    private static $plural_name = 'Downloadable Products';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
33
34
    private static $description = "A product that can be downloaded";
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
35
36
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
37
        'LinkLife' => 'Int'
38
    ];
39
40
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
41
        "File" => File::class
42
    ];
43
44
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
45
        "DownloadLink" => "Varchar",
46
        "Deliverable" => "Boolean"
47
    ];
48
49
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
50
        "File"
51
    ];
52
53
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
54
        'LinkLife' => 7
55
    ];
56
57
    /**
58
     * Downloadable products are not deliverable. This will be
59
     * detected by the shopping cart to disable delivery options.
60
     *
61
     * @return boolean
62
     */
63
    public function getDeliverable()
64
    {
65
        return false;
66
    }
67
68
    /**
69
     * Get the link to download the file associated with this product
70
     *
71
     * @param int    $invoice_id the ID of an associated invoice
72
     * @param string $access_key key of assocaiated invoice
73
     * 
74
     * @return string
75
     */
76
    public function getDownloadLink($invoice_id, $access_key)
77
    {
78
        $file = $this->File();
79
80
        if ($file->exists()) {
81
            $download = Injector::inst()
82
                ->get(FileDownloadController::class);
83
84
            return $download->DownloadLink(
85
                $file->ID,
86
                $invoice_id,
87
                $access_key,
88
                $file->Name
89
            );
90
        }
91
92
        return "";
93
    }
94
95
    /**
96
     * Get the folder to add downloads to
97
     * 
98
     * @return DownloadFolder
99
     */
100
    public function getDownloadFolder()
101
    {
102
        return DownloadFolder::find_or_make(
103
            Config::inst()->get(DownloadFolder::class, "folder_name")
104
        );
105
    }
106
107
108
    public function getCMSFields()
109
    {
110
        $fields = parent::getCMSFields();
111
112
        $fields->removeByName("Weight");
113
114
        $fields->addFieldsToTab(
115
            "Root.Settings",
116
            [
117
                TextField::create('LinkLife', 'Life of download link (in days)'),
118
                UploadField::create("File")
119
                    ->setFolderName($this->config()->folder_name)
120
            ]
121
        );
122
123
        return $fields;
124
    }
125
126
    /**
127
     * Ensure weight is removed on save and that attached files are moved to
128
     * the correct folder
129
     *
130
     * @return void
131
     */
132
    public function onBeforeWrite()
133
    {
134
        parent::onBeforeWrite();
135
136
        // Set weight
137
        $this->Weight = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property Weight does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
138
139
        // Deal with moving file
140
        $file = $this->File();
141
        $folder = $this->getDownloadFolder();
142
        $move = true;
143
144
        // If the file is in the download folder (or an ancestor), don't move
145
        if ($file->exists() && $file->ParentID == $folder->ID) {
146
            $move = false;
147
        } elseif ($file->exists()) {
148
            $id_list = $folder->getDescendantIDList();
0 ignored issues
show
Bug introduced by
The method getDescendantIDList() does not exist on SilverCommerce\DownloadableProducts\DownloadFolder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

148
            /** @scrutinizer ignore-call */ 
149
            $id_list = $folder->getDescendantIDList();
Loading history...
149
            if (in_array($file->ParentID, $id_list)) {
150
                $move = false;
151
            }
152
        }
153
154
        // If needed, move the attached file to a new folder
155
        if ($move) {
156
            $file->ParentID = $folder->ID;
157
            $file->write();
158
        }
159
    }
160
161
    /**
162
     * Special permission to see if this product can be downloaded by the current member
163
     *
164
     * @param Member $member The current member object
165
     *
166
     * @return boolean
167
     */
168
    public function canDownload(Member $member = null)
169
    {
170
        if (!$member) {
171
            $member = Security::getCurrentUser();
172
        }
173
174
        $contact = null;
175
176
        if (isset($member)) {
177
            $contact = $member->Contact();
178
        }
179
180
        if (isset($contact)) {
181
            $items = $contact
182
                ->Invoices()
183
                ->filter(
184
                    [
185
                        "Status" => Config::inst()
186
                            ->get(Invoice::class, "paid_statuses"),
187
                        "Items.StockID" => $this->StockID
188
                    ]
189
                );
190
191
            return $items->exists();
192
        }
193
194
        return false;
195
    }
196
}
197