graphAPIAdapter = $graphAPIAdapter; $this->systemConfig = $systemConfig; $this->fbeHelper = $fbeHelper; $this->resourceConnection = $resourceConnection; } /** * Execute the CRON job * * @return void */ public function execute() { foreach ($this->systemConfig->getAllFBEInstalledStores() as $store) { $storeId = $store->getId(); try { $accessToken = $this->systemConfig->getAccessToken($storeId); $this->graphAPIAdapter->persistLogToMeta( [ 'event' => 'log_catalog_setup_data', 'event_type' => 'log_catalog_setup', 'catalog_id' => $this->systemConfig->getCatalogId($storeId), 'commerce_merchant_settings_id' => $this->systemConfig->getCommerceAccountId($storeId), 'extra_data' => [ 'items' => json_encode( [ 'item_count' => $this->queryItemCount(), 'group_count' => $this->queryGroupCount(), 'breakdown' => $this->queryBreakdown(), ] ) ] ], $accessToken ); } catch (\Exception $e) { $this->fbeHelper->logExceptionImmediatelyToMeta($e, [ 'store_id' => $storeId, 'event' => 'log_catalog_setup_cron_exception', 'event_type' => 'log_catalog_setup' ]); } } } /** * Queries the product count breakdown * * @return array */ private function queryBreakdown() { $productTable = $this->resourceConnection->getTableName('catalog_product_entity'); $storeTable = $this->resourceConnection->getTableName('store'); $attributes = $this->resourceConnection->getTableName('catalog_product_entity_int'); $sendToFacebookId = $this->queryAttributeId('send_to_facebook'); $statusId = $this->queryAttributeId('status'); $visibilityId = $this->queryAttributeId('visibility'); $connection = $this->resourceConnection->getConnection(); return $connection ->select() ->from(['store' => $storeTable], []) ->join(['product' => $productTable], "1 = 1", []) ->joinLeft( ['send_to_facebook' => $attributes], "product.entity_id = send_to_facebook.entity_id" ." AND store.store_id = send_to_facebook.store_id" ." AND send_to_facebook.attribute_id = {$sendToFacebookId}", [] ) ->joinLeft( ['status' => $attributes], "product.entity_id = status.entity_id" ." AND store.store_id = status.store_id" ." AND status.attribute_id = {$statusId}", [] ) ->joinLeft( ['visibility' => $attributes], "product.entity_id = visibility.entity_id" ." AND store.store_id = visibility.store_id" ." AND visibility.attribute_id = {$visibilityId}", [] ) ->group([ 'store.store_id', 'store.name', 'product.type_id', 'status.value', 'visibility.value', 'send_to_facebook.value' ]) ->columns([ 'store_id' => 'store.store_id', 'store_name' => 'store.name', 'type_id' => 'product.type_id', 'status' => 'status.value', 'visibility' => 'visibility.value', 'send_to_facebook' => 'send_to_facebook.value', 'count' => 'count(*)', ]) ->query() ->fetchAll(); } /** * Queries the number of product items * * @return int * @throws \Zend_Db_Statement_Exception */ private function queryItemCount() { $productsTable = $this->resourceConnection->getTableName('catalog_product_entity'); $result = $this->resourceConnection ->getConnection() ->select() ->from($productsTable) ->columns('count(*) AS count') ->query() ->fetch(); return $result['count']; } /** * Queries the number of product groups * * @return int * @throws \Zend_Db_Statement_Exception */ private function queryGroupCount() { $productsTable = $this->resourceConnection->getTableName('catalog_product_entity'); $linksTable = $this->resourceConnection->getTableName('catalog_product_super_link'); $result = $this->resourceConnection ->getConnection() ->select() ->from($productsTable, []) ->joinLeft( ['link' => $linksTable], 'entity_id = link.product_id', [] ) ->where('link.product_id IS NULL') ->columns('count(*) AS count') ->query() ->fetch(); return $result['count']; } /** * Queries the id of an attribute * * @param string $code * @return int * @throws \Zend_Db_Statement_Exception */ private function queryAttributeId(string $code) { $attributesTable = $this->resourceConnection->getTableName('eav_attribute'); $result = $this->resourceConnection->getConnection()->select()->from($attributesTable) ->columns('attribute_id') ->where('attribute_code = ?', $code) ->limit(1) ->query() ->fetch(); return $result['attribute_id']; } }