Blame view

components/OrderProductLogger.php 1.51 KB
17569d93   Alexey Boroda   -Order product lo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  <?php
      namespace artweb\artbox\ecommerce\components;
      
      use artweb\artbox\ecommerce\models\OrderProductLog;
      use yii\base\Object;
      use yii\helpers\Json;
  
      class OrderProductLogger extends Object implements LoggerInterface
      {
          /**
           * @param array $changedAttributes
           * @param array $oldAttributes
           * @param bool  $insert
           *
           * @return array
           */
          public static function generateData(array $changedAttributes, array $oldAttributes, bool $insert)
          {
              $data = [];
              foreach ($changedAttributes as $key => $attribute) {
                  if ($oldAttributes[ $key ] != $attribute) {
                      $data[ $key ] = [
                          'old' => $attribute,
                          'new' => $oldAttributes[ $key ],
                      ];
                  }
              }
              
              return $data;
          }
      
          /**
           * @param array $data
           * @param int   $identityId
           * @param array $params
           */
          public static function saveData(array $data, int $identityId, $params = [])
          {
              if (!empty($data)) {
                  $log = new OrderProductLog();
                  $log->order_product_id = (integer) $identityId;
                  $log->order_id = $params['order_id'];
                  $log->user_id = (integer) \Yii::$app->user->identity->id;
                  $log->data = Json::encode($data);
                  
                  $log->save();
              }
          }
      }