Completed
Push — development ( 1edf8d...636245 )
by Ashutosh
10:23
created

AutoUpdateController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 45
dl 0
loc 89
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A postCurl() 0 14 1
A __construct() 0 7 1
A searchVersion() 0 19 5
A addNewVersion() 0 5 1
A addNewProductToAUS() 0 6 1
A editVersion() 0 8 1
1
<?php
2
3
namespace App\Http\Controllers\AutoUpdate;
4
5
use App\ApiKey;
6
use App\Http\Controllers\Controller;
7
8
class AutoUpdateController extends Controller
9
{
10
    private $api_key_secret;
11
    private $url;
12
    private $update;
13
14
    public function __construct()
15
    {
16
        $model = new ApiKey();
17
        $this->update = $model->firstOrFail();
18
19
        $this->api_key_secret = $this->update->update_api_secret;
20
        $this->url = $this->update->update_api_url;
21
    }
22
23
    private function postCurl($post_url, $post_info)
24
    {
25
        $ch = curl_init();
26
        curl_setopt($ch, CURLOPT_URL, $post_url);
27
        curl_setopt($ch, CURLOPT_POST, 1);
28
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_info);
29
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
30
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
31
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
32
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
33
        $result = curl_exec($ch);
34
        curl_close($ch);
35
36
        return $result;
37
    }
38
39
    /*
40
    *  Add New Product
41
    */
42
    public function addNewProductToAUS($product_name, $product_sku)
43
    {
44
        $url = $this->url;
45
        $key = str_random(16);
46
        $api_key_secret = $this->api_key_secret;
47
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=products_add&product_title=$product_name&product_sku=$product_sku&product_key=$key&product_status=1");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 191 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
48
    }
49
50
51
52
    /*
53
    *  Add New Version
54
    */
55
    public function addNewVersion($product_id, $version_number, $upgrade_zip_file, $version_status)
56
    {
57
        $url = $this->url;
58
        $api_key_secret = $this->api_key_secret;
59
        $addNewVersion = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=versions_add&product_id=$product_id&version_number=$version_number&version_upgrade_file=$upgrade_zip_file&version_status=$version_status&product_status=1");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 248 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
60
    }
61
62
    /*
63
    *  Edit Version
64
    */
65
    public function editVersion($version_number, $product_sku)
66
    {
67
        $url = $this->url;
68
        $api_key_secret = $this->api_key_secret;
69
        $searchLicense = $this->searchVersion($version_number, $product_sku);
70
        $versionId = $searchLicense['version_id'];
71
        $productId = $searchLicense['product_id'];
72
        $addNewVersion = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=versions_edit&product_id=productId&version_id=$versionId&version_number=$version_number&version_status=1");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 199 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
73
    }
74
75
    /*
76
    *  Search Version
77
    */
78
    public function searchVersion($version_number, $product_sku)
79
    {
80
        $versionId = '';
81
        $productId = '';
82
        $url = $this->url;
83
        $api_key_secret = $this->api_key_secret;
84
        $getVersion = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
85
        &search_type=version&search_keyword=$product_sku");
86
        $details = json_decode($getVersion);
87
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {
88
            foreach ($details->page_message as $detail) {
89
                if ($detail->version_number == $version_number) {
90
                    $versionId = $detail->version_id;
91
                    $productId = $detail->product_id;
92
                }
93
            }
94
        }
95
96
        return ['version_id'=>$versionId, 'product_id'=>$productId];
97
    }
98
}
99