Completed
Push — main ( 02b4a5...a91cd2 )
by Dylan
02:26 queued 02:26
created

Cron::fetch_products()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Examples\Controllers;
4
5
use Examples\Models\Store;
6
use Lifeboat\App;
7
use Lifeboat\Exceptions\ApiException;
8
use Lifeboat\Exceptions\OAuthException;
9
use Lifeboat\Models\Product;
10
use Lifeboat\Resource\ListResource;
11
12
/**
13
 * Class Cron
14
 * @package Examples\Controllers
15
 *
16
 * An example controller showcasing how you may use the Lifeboat SDK
17
 * when a user is unable to interact with your app
18
 */
19
class Cron extends Controller {
0 ignored issues
show
Bug introduced by
The type Examples\Controllers\Controller 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...
20
21
    const LIFEBOAT_APP_ID       = '[[Lifeboat App ID]]';
22
    const LIFEBOAT_APP_SECRET   = '[[Lifeboat App Secret]]';
23
24
    /**
25
     * In this function we demonstrate fetching products
26
     * without an action session.
27
     *
28
     * Note: We're passing the Store object that contains $SiteKey & $Host
29
     * These parameters are required when making request without an active session
30
     *
31
     * Note: The SDK will automatically request, refresh and cache access token.
32
     * You don't need to call the @see App::fetchAccessToken()
33
     *
34
     * @see Auth::process() - To see how we're creating and setting the Store object
35
     * @see Store::find_or_make() - To see how we would retreive a store
36
     *
37
     * @param Store $store
38
     * @return ListResource
39
     */
40
    public static function fetch_products(Store $store): ListResource
41
    {
42
        // Initialise the SDK
43
        $app = new App(self::LIFEBOAT_APP_ID, self::LIFEBOAT_APP_SECRET);
44
45
        // Set the site we'll be interacting with
46
        $app->setActiveSite($store->getSiteHost(), $store->getSiteKey());
47
48
        // Get an iterator for all the products in $store
49
        return $app->products->all();
50
    }
51
52
    /**
53
     * In this example we'll show to set a fixed price to all the products
54
     * in a $store without an active session
55
     *
56
     * @param Store $store
57
     * @param float $price
58
     * @return void
59
     */
60
    public static function set_new_price(Store $store, float $price)
61
    {
62
        // Initialise the SDK
63
        $app = new App(self::LIFEBOAT_APP_ID, self::LIFEBOAT_APP_SECRET);
64
65
        // Set the site we'll be interacting with
66
        $app->setActiveSite($store->getSiteHost(), $store->getSiteKey());
67
68
        /** @var Product $product */
69
        foreach ($app->products->all() as $product) {
70
            try {
71
                $app->products->update($product->ID, [
72
                    'Price' => $price
73
                ]);
74
            } catch (OAuthException $auth) {
75
                // Will be thrown if your app is not authorised to access this store
76
                error_log($auth);
77
            } catch (ApiException $api) {
78
                // Will be thrown if you've provided invalid data to the API
79
                error_log($api);
80
            }
81
        }
82
    }
83
}
84