Skip to content

Commit

Permalink
Merge pull request #4223 in SW/shopware from SW-17001/5.2/Implements-…
Browse files Browse the repository at this point in the history
…FRW-API-call to 5.2

* commit 'f9d6be4396da6f7440a7a34e1287965adfb741ce':
  SW-17001 - Adds tracking to InstallCommand
  SW-17001 - Refactors unique id generation, adds notification calls
  • Loading branch information
bcremer committed Nov 28, 2016
2 parents 0cb941b + f9d6be4 commit cf65911
Show file tree
Hide file tree
Showing 16 changed files with 567 additions and 19 deletions.
22 changes: 22 additions & 0 deletions _sql/migrations/796-add-uniqueid-config-element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class Migrations_Migration796 extends Shopware\Components\Migrations\AbstractMigration
{
public function up($modus)
{
$sql = <<<SQL
INSERT INTO s_core_config_elements (form_id, name, value, label, description, type, required, position, scope)
VALUES ('0', 'trackingUniqueId', 's:0:"";', 'Unique identifier', '', 'text', '0', '0', '1')
SQL;

$statement = $this->getConnection()->prepare("SELECT id FROM s_core_config_elements WHERE name = 'update-unique-id'");
$statement->execute();
$id = $statement->fetchColumn();

if (!empty($id)) {
$sql = 'UPDATE s_core_config_elements SET name="trackingUniqueId" WHERE id=' . $this->getConnection()->quote($id);
}

$this->addSql($sql);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Shopware 5
* Copyright (c) shopware AG.
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, title and interest in
* our trademarks remain entirely with us.
*/
namespace Shopware\Bundle\PluginInstallerBundle\Service\UniqueIdGenerator;

use Doctrine\DBAL\Connection;
use Shopware\Components\Random;
use Shopware\Bundle\PluginInstallerBundle\Service\UniqueIdGeneratorInterface;

/**
* Class UniqueIdGenerator.
*
* A simple class for storing a generated unique Id in the database.
*/
class UniqueIdGenerator implements UniqueIdGeneratorInterface
{
/**
* @var Connection
*/
private $connection;

/**
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* @return string
*/
public function getUniqueId()
{
if ($uniqueId = $this->readUniqueIdFromDb()) {
return $uniqueId;
}

$uniqueId = Random::getAlphanumericString(32);

$this->storeUniqueIdInDb($uniqueId);

return $uniqueId;
}

/**
* @return string
*/
private function readUniqueIdFromDb()
{
$sql = <<<'sql'
SELECT s_core_config_values.value FROM s_core_config_values
INNER JOIN s_core_config_elements
ON s_core_config_values.element_id = s_core_config_elements.id
AND s_core_config_elements.name LIKE 'trackingUniqueId'
WHERE s_core_config_values.shop_id = 1
sql;
$uniqueId = $this->connection->fetchColumn($sql);

if ($uniqueId !== false && is_string($uniqueId)) {
return unserialize($uniqueId);
}

return null;
}

/**
* @param string $uniqueId
*/
private function storeUniqueIdInDb($uniqueId)
{
$sql = <<<'sql'
INSERT INTO s_core_config_values (element_id, shop_id, value) VALUES (
(SELECT id FROM s_core_config_elements WHERE name LIKE 'trackingUniqueId' LIMIT 1), 1, :value
)
sql;

$this->connection->executeUpdate(
$sql, [
'value' => serialize($uniqueId),
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Shopware 5
* Copyright (c) shopware AG.
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, title and interest in
* our trademarks remain entirely with us.
*/
namespace Shopware\Bundle\PluginInstallerBundle\Service;

/**
* Interface UniqueIdInterface.
*
* A simple interface for services capable of generating unique ids.
*/
interface UniqueIdGeneratorInterface
{
/**
* @return string
*/
public function getUniqueId();
}
43 changes: 38 additions & 5 deletions engine/Shopware/Bundle/PluginInstallerBundle/StoreClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Shopware\Bundle\PluginInstallerBundle\Exception\SbpServerException;
use Shopware\Bundle\PluginInstallerBundle\Exception\ShopSecretException;
use Shopware\Bundle\PluginInstallerBundle\Exception\StoreException;
use Shopware\Bundle\PluginInstallerBundle\Service\UniqueIdGeneratorInterface;
use Shopware\Bundle\PluginInstallerBundle\Struct\AccessTokenStruct;
use Shopware\Components\HttpClient\HttpClientInterface;
use Shopware\Components\HttpClient\RequestException;
Expand Down Expand Up @@ -64,21 +65,29 @@ class StoreClient
private $openSSLVerifier;

/**
* @param HttpClientInterface $httpClient
* @param string $apiEndPoint
* @param Struct\StructHydrator $structHydrator
* @param OpenSSLVerifier $openSSLVerifier
* @var UniqueIdGeneratorInterface
*/
private $uniqueIdGenerator;

/**
* @param HttpClientInterface $httpClient
* @param string $apiEndPoint
* @param Struct\StructHydrator $structHydrator
* @param OpenSSLVerifier $openSSLVerifier
* @param UniqueIdGeneratorInterface $uniqueIdGenerator
*/
public function __construct(
HttpClientInterface $httpClient,
$apiEndPoint,
Struct\StructHydrator $structHydrator,
OpenSSLVerifier $openSSLVerifier
OpenSSLVerifier $openSSLVerifier,
UniqueIdGeneratorInterface $uniqueIdGenerator
) {
$this->httpClient = $httpClient;
$this->apiEndPoint = $apiEndPoint;
$this->structHydrator = $structHydrator;
$this->openSSLVerifier = $openSSLVerifier;
$this->uniqueIdGenerator = $uniqueIdGenerator;
}

/**
Expand Down Expand Up @@ -255,6 +264,30 @@ public function doPing()
return json_decode($response->getBody(), true) ?: false;
}

/**
* @param string $eventName
* @param array $additionalData
*
* @return array|false
*/
public function doTrackEvent($eventName, $additionalData = [])
{
$payload = [
'additionalData' => $additionalData,
'instanceId' => $this->uniqueIdGenerator->getUniqueId(),
'event' => $eventName,
];

try {
$response = $this->httpClient->post($this->apiEndPoint.'/tracking/events', ['timeout' => 7], json_encode($payload));
$this->verifyResponseSignature($response);
} catch (RequestException $ex) {
return false;
}

return json_decode($response->getBody(), true) ?: false;
}

/**
* @param $resource
* @param array $params
Expand Down
5 changes: 5 additions & 0 deletions engine/Shopware/Bundle/PluginInstallerBundle/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<argument>%shopware.store.apiEndpoint%</argument>
<argument type="service" id="shopware_plugininstaller.plugin_installer_struct_hydrator" />
<argument type="service" id="shopware.openssl_verificator"/>
<argument type="service" id="shopware_plugininstaller.unique_id_generator" />
</service>

<service id="shopware_plugininstaller.unique_id_generator" class="Shopware\Bundle\PluginInstallerBundle\Service\UniqueIdGenerator\UniqueIdGenerator">
<argument type="service" id="dbal_connection" />
</service>

<service id="shopware_plugininstaller.plugin_installer" class="Shopware\Bundle\PluginInstallerBundle\Service\PluginInstaller">
Expand Down
4 changes: 4 additions & 0 deletions engine/Shopware/Controllers/Backend/FirstRunWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function saveEnabledAction()
)
);

/** @var \Shopware\Bundle\PluginInstallerBundle\StoreClient $storeClient */
$storeClient = $this->container->get('shopware_plugininstaller.store_client');
$storeClient->doTrackEvent('First Run Wizard completed');

$this->Request()->setParam('elements', $requestElements);

$this->forward('saveForm', 'Config');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ public function getLocalizationsAction()
return;
}

/** @var \Shopware\Bundle\PluginInstallerBundle\StoreClient $storeClient */
$storeClient = $this->container->get('shopware_plugininstaller.store_client');
$storeClient->doTrackEvent('First Run Wizard started');

$this->View()->assign(array(
'success' => true,
'data' => $localizations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,21 +447,10 @@ private function checkIdententical($fp1, $fp2)
*/
private function getUnique()
{
$config = $this->getPluginConfig();

if (isset($config['update-unique-id']) && !empty($config['update-unique-id'])) {
return $config['update-unique-id'];
}

$uniqueid = Random::getAlphanumericString(32);

$shop = $this->get('models')->getRepository('Shopware\Models\Shop\Shop')->findOneBy(array('default' => true));

$pluginManager = $this->container->get('shopware_plugininstaller.plugin_manager');
$plugin = $pluginManager->getPluginByName('SwagUpdate');
$pluginManager->saveConfigElement($plugin, 'update-unique-id', $uniqueid, $shop);
/** @var UniqueIdGeneratorInterface $uniqueIdGenerator */
$uniqueIdGenerator = $this->container->get('shopware_plugininstaller.unique_id_generator');

return $uniqueid;
return $uniqueIdGenerator->getUniqueId();
}

/**
Expand Down
79 changes: 79 additions & 0 deletions recovery/common/src/Service/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Shopware 5
* Copyright (c) shopware AG
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, title and interest in
* our trademarks remain entirely with us.
*/

namespace Shopware\Recovery\Common\Service;

use Shopware\Recovery\Common\HttpClient\Client;

class Notification
{
/**
* @var string
*/
private $apiEndPoint;

/**
* @var Client
*/
private $client;

/**
* @var string
*/
private $uniqueId;

/**
* @param string $apiEndPoint
* @param string $uniqueId
* @param Client $client
*/
public function __construct($apiEndPoint, $uniqueId, Client $client)
{
$this->apiEndPoint = $apiEndPoint;
$this->client = $client;
$this->uniqueId = $uniqueId;
}

/**
* @param string $eventName
* @param array $additionalInformation
* @return array|false
*/
public function doTrackEvent($eventName, $additionalInformation = [])
{
$payload = [
'additionalData' => $additionalInformation,
'instanceId' => $this->uniqueId,
'event' => $eventName,
];

try {
$response = $this->client->post($this->apiEndPoint.'/tracking/events', json_encode($payload));
} catch (\Exception $ex) {
return false;
}

return json_decode($response->getBody(), true) ?: false;
}
}
Loading

0 comments on commit cf65911

Please sign in to comment.