示例#1
0
 /**
  * Gets the Rate based on the type of record i.e 'publisher', or 'advertiser'
  * @param  array  $commissions Array of Commissions to search for ad_id
  * @param  String $type        Advertiser | Publisher
  * @return array
  */
 public static function campaignRate($adid, &$commissions = [], $country = null, $extra = [])
 {
     $commFetched = $extra['commFetched'] ?? false;
     if ($commFetched) {
         $comm = $commissions;
     } else {
         $comm = self::find($commissions, $adid);
     }
     $info = ['campaign' => 'cpc', 'rate' => 0, 'revenue' => 0, 'type' => $extra['type']];
     if (!is_array($comm)) {
         return $info;
     }
     $commission = array_key_exists($country, $comm) ? $comm[$country] : @$comm['ALL'];
     // because commission might not exists if country is null
     if (!is_object($commission)) {
         return $info;
     }
     $query = ['adid' => $adid, 'created' => Db::dateQuery($extra['start'], $extra['end'])];
     switch ($extra['type']) {
         case 'advertiser':
             $info['revenue'] = (double) $commission->revenue;
             break;
         case 'publisher':
             $info['rate'] = self::getPubRate($commission, $extra);
             $query['pid'] = $extra['publisher']->_id ?? null;
             break;
         case 'both':
             $info['revenue'] = (double) $commission->revenue;
             $info['rate'] = self::getPubRate($commission, $extra);
             if (isset($extra['publisher'])) {
                 $query['pid'] = $extra['publisher']->_id;
             }
             break;
     }
     switch (strtolower($commission->model)) {
         case 'cpa':
         case 'cpi':
             $count = \Conversion::count($query);
             $info['conversions'] = $count;
             break;
         case 'cpm':
             $info['impressions'] = \Impression::getStats($query);
             break;
     }
     $info['campaign'] = strtolower($commission->model);
     return $info;
 }