EcommerceTaskDeleteProducts   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 20 5
1
<?php
2
3
/**
4
 * @description: see description
5
 *
6
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
7
 * @package: ecommerce
8
 * @sub-package: tasks
9
 * @inspiration: Silverstripe Ltd, Jeremy
10
 **/
11
class EcommerceTaskDeleteProducts extends BuildTask
12
{
13
    private static $allowed_actions = array(
14
        '*' => 'ADMIN',
15
    );
16
17
    protected $title = 'Delete e-commerce Buyables';
18
19
    protected $description = 'Removes all Buyables (Products) from the database.';
20
21
    public function run($request)
22
    {
23
        $arrayOfBuyables = EcommerceConfig::get('EcommerceDBConfig', 'array_of_buyables');
24
        foreach ($arrayOfBuyables as $buyable) {
0 ignored issues
show
Bug introduced by
The expression $arrayOfBuyables of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
25
            $allproducts = $buyable::get();
26
            if ($allproducts->count()) {
27
                foreach ($allproducts as $product) {
28
                    DB::alteration_message('Deleting '.$product->ClassName.' ID = '.$product->ID, 'deleted');
29
                    if (is_a($product, Object::getCustomClass('SiteTree'))) {
30
                        $product->deleteFromStage('Live');
31
                        $product->deleteFromStage('Draft');
32
                    } else {
33
                        $product->delete();
34
                    }
35
                    $product->destroy();
36
                    //TODO: remove versions
37
                }
38
            }
39
        }
40
    }
41
}
42