Skip to content

Commit

Permalink
Merge pull request #1 from A5sys/first-commit
Browse files Browse the repository at this point in the history
first commit
  • Loading branch information
arnaudgoulpeau authored Dec 1, 2016
2 parents dc33870 + 68530c1 commit c85bbc3
Show file tree
Hide file tree
Showing 14 changed files with 783 additions and 0 deletions.
133 changes: 133 additions & 0 deletions CookedMeasure/Coordinate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace QowisioCloudApiBundle\CookedMeasure;

/**
* Represents a GPS coordinate, with latitude and longitude
*/
class Coordinate implements \JsonSerializable
{
protected $latitude;

protected $longitude;

protected $timestampCollected;

/**
* Constructor
* @param mixed string|float $latitude
* @param mixed string|float $longitude
* @param string $timestampCollected
*/
public function __construct($latitude, $longitude, $timestampCollected)
{
$this
->setLatitude($latitude)
->setLongitude($longitude)
->setTimestampCollected($timestampCollected)
;
}

/**
*
* @return float
*/
function getLatitude()
{
return $this->latitude;
}

/**
*
* @return float
*/
function getLongitude()
{
return $this->longitude;
}

/**
*
* @return integer
*/
function getTimestampCollected()
{
return $this->timestampCollected;
}

/**
* converts the timestampCollected to a \DateTime object
* The date is computed from the given timestamp, but is converted in the given timezone (system timezone if not provided).
* Use getUtcDateCollected to get an UTC date
* @param $timezone the timezone to use. System timezone if null or not provided
* @return \DateTime
*/
function getDateCollected($timezone = null)
{
$date = new \DateTime('@'.$this->timestampCollected);
$date->setTimezone(new \DateTimeZone($timezone ? $timezone : date_default_timezone_get()));

return $date;
}

/**
* converts the timestampCollected to a \DateTime object
* Warning, the date is given UTC
* @return \DateTime
*/
function getUtcDateCollected()
{
return new \DateTime('@'.$this->timestampCollected);
}

/**
*
* @param mixed string|float $latitude
* @return \QowisioCloudApiBundle\CookedMeasure\Coordinate
*/
function setLatitude($latitude)
{
$this->latitude = floatval($latitude);

return $this;
}

/**
*
* @param mixed string|float $longitude
* @return \QowisioCloudApiBundle\CookedMeasure\Coordinate
*/
function setLongitude($longitude)
{
$this->longitude = floatval($longitude);

return $this;
}

/**
*
* @param string $timestampCollected
* @return \QowisioCloudApiBundle\CookedMeasure\Coordinate
*/
function setTimestampCollected($timestampCollected)
{
$this->timestampCollected = intval($timestampCollected);

return $this;
}

/**
* Implementation of JsonSerializable needed for json_encode
* @return array
*/
public function jsonSerialize()
{
return [
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'timestampCollected' => $this->timestampCollected,
'dateCollected' => $this->getDateCollected(),
'dateUtcCollected' => $this->getUtcDateCollected(),
];
}
}
43 changes: 43 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace QowisioCloudApiBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('qowisio_cloud_api');

$rootNode
->children()
->arrayNode('authentication')
->children()
->scalarNode('email')->isRequired()->end()
->scalarNode('password')->isRequired()->end()
->end()
->end()
->arrayNode('endpoints')
->addDefaultsIfNotSet()
->children()
->scalarNode('authentication')->defaultValue('https://auth.qowisio.com/')->end()
->scalarNode('data')->defaultValue('https://api.qowisio.com/')->end()
->end()
->end()
->end()
;

return $treeBuilder;
}
}
34 changes: 34 additions & 0 deletions DependencyInjection/QowisioCloudApiExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace QowisioCloudApiBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class QowisioCloudApiExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

$container->setParameter('qowisio.cloud.api.auth.endpoint', $config['endpoints']['authentication']);
$container->setParameter('qowisio.cloud.api.data.endpoint', $config['endpoints']['data']);

$container->setParameter('qowisio.cloud.api.auth.email', $config['authentication']['email']);
$container->setParameter('qowisio.cloud.api.auth.password', $config['authentication']['password']);
}
}
10 changes: 10 additions & 0 deletions Exception/QowisioCredentialsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace QowisioCloudApiBundle\Exception;

/**
* When credentials are wrong
*/
class QowisioCredentialsException extends QowisioException
{
}
10 changes: 10 additions & 0 deletions Exception/QowisioException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace QowisioCloudApiBundle\Exception;

/**
* When an error occurs into the bundle
*/
class QowisioException extends \Exception
{
}
12 changes: 12 additions & 0 deletions QowisioCloudApiBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace QowisioCloudApiBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Qowisio Cloud API Bundle
*/
class QowisioCloudApiBundle extends Bundle
{
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Qowisio Cloud API Bundle ##
45 changes: 45 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
parameters:
# Service to call Qowisio WS
qowisio.cloud.api.caller.class: 'QowisioCloudApiBundle\Service\QowisioApiCaller'
# Service to get auth infos
qowisio.cloud.api.authentication.class: 'QowisioCloudApiBundle\Service\QowisioApiAuthenticationService'
# Service to get devices and sensors data
qowisio.cloud.api.devices.and.sensors.class: 'QowisioCloudApiBundle\Service\QowisioApiDevicesAndSensorsService'
# Service to get measures data
qowisio.cloud.api.measures.class: 'QowisioCloudApiBundle\Service\QowisioApiMeasuresService'
# Service to get data from a GPS device (named tracker)
qowisio.tracker.class: 'QowisioCloudApiBundle\Service\QowisioTrackerService'

services:
# makes the calls to API
qowisio.cloud.api.caller:
class: %qowisio.cloud.api.caller.class%
arguments:
- %qowisio.cloud.api.auth.endpoint%
- %qowisio.cloud.api.auth.email%
- %qowisio.cloud.api.auth.password%
- %qowisio.cloud.api.data.endpoint%

# Get infos about authentication
qowisio.cloud.api.authentication:
class: %qowisio.cloud.api.authentication.class%
arguments:
- "@qowisio.cloud.api.caller"

# Get infos about devices and their sensors
qowisio.cloud.api.devices.and.sensors:
class: %qowisio.cloud.api.devices.and.sensors.class%
arguments:
- "@qowisio.cloud.api.caller"

# Get infos about sensor measures
qowisio.cloud.api.measures:
class: %qowisio.cloud.api.measures.class%
arguments:
- "@qowisio.cloud.api.caller"

# Get info about the specific tracker device
qowisio.tracker:
class: %qowisio.tracker.class%
arguments:
- "@qowisio.cloud.api.measures"
46 changes: 46 additions & 0 deletions Service/QowisioApiAuthenticationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace QowisioCloudApiBundle\Service;

use QowisioCloudApiBundle\Exception\QowisioException;

/**
* Test if your user is authenticated
* Note that the process of authentication itself is NOT done in this service since it is needed for every single api calls, with a JSON Web Token (JWT). @see QowisioApiCaller
* ref: qowisio.cloud.api.authentication
*/
class QowisioApiAuthenticationService
{
const FUNCTION_AMIAUTHENTICATED = 'amiauthenticated';

/**
* WS API caller
* @var QuowisioApiCaller
*/
protected $apiCaller;

/**
* Constructor
* @param \QowisioCloudApiBundle\Service\QowisioApiCaller $apiCaller
*/
public function __construct(QowisioApiCaller $apiCaller)
{
$this->apiCaller = $apiCaller;
}

/**
* Is the user connected to Qowisio Cloud API
* @return boolean true if authentication on the Quowisio Cloud API
*/
public function amIAuthenticated()
{
$isAuthenticated = true;
try {
$this->apiCaller->callWs(static::FUNCTION_AMIAUTHENTICATED);
} catch (QowisioException $ex) {
$isAuthenticated = false;
}

return $isAuthenticated;
}
}
Loading

0 comments on commit c85bbc3

Please sign in to comment.