PHP code example of devhelp / piwik-silex-provider

1. Go to this page and download the library: Download devhelp/piwik-silex-provider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devhelp / piwik-silex-provider example snippets


$app = new Silex\Application();

$app->register(new Devhelp\Silex\Piwik\PiwikApiServiceProvider(array(
    'client' => 'my_piwik.client',
    'api' => array(
        'reader' => array(
            'url' => 'http://my_piwik_instance.piwik.pro',
            'default_params' => array(
                'token_auth' => 'piwik_token_auth',
                'idSite' => 123
            )
        )
    )
)));

//'guzzle' service must implement GuzzleHttp\ClientInterface
$app['my_piwik.client'] = $app->share(function () use ($app) {
    return new Devhelp\Piwik\Api\Guzzle\Client\PiwikGuzzleClient($app['guzzle']));
});

$app['my_service'] = $app->share(function () use ($app) {
    return new Acme\DemoBundle\Service\MyService($app['devhelp_piwik.api']);
});

namespace Acme\DemoBundle\Service;


use Devhelp\Piwik\Api\Api;

class MyService
{

    /**
     * @var Api
     */
    private $piwikApi;

    public function __construct(Api $piwikApi)
    {
        $this->piwikApi = $piwikApi;
    }

    public function doSomething()
    {
        //...
        $this->piwikApi->getMethod('PiwikPlugin.pluginAction')->call();
        //...
    }
}

$app = new Silex\Application();

$app->register(new Devhelp\Silex\Piwik\PiwikApiServiceProvider(array(
    'client' => 'my_piwik.client',
    'api' => array(
        'reader' => array(
            'url' => 'http://my_piwik_instance.piwik.pro',
            'default_params' => array(
                'token_auth' => 'my_token_auth_provider',
                'idSite' => 123
            )
        )
    )
)));

$app['my_token_auth_provider'] = $app->share(function () use ($app) {
    return new Acme\DemoBundle\Param\MyTokenAuthProvider($app['security.token_storage']);
});

namespace Acme\DemoBundle\Param;

use Devhelp\Piwik\Api\Param\Param;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class MyTokenAuthProvider implements Param
{

    /**
     * @var TokenStorageInterface
     */
    private $tokenStorage;

    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function value()
    {
        $token = $this->tokenStorage->getToken();

        return $token instanceof TokenInterface ? $token->getUser()->getPiwikToken() : null;
    }
}

$app['my_piwik_method'] = $app->share(function () use ($app) {
    return $app['devhelp_piwik.api']->getMethod('VisitFrequency.get');
});