Skip to content

Instantly share code, notes, and snippets.

@Hounddog
Last active August 29, 2015 14:02
Show Gist options
  • Save Hounddog/39e0cb667d2dd1f2f946 to your computer and use it in GitHub Desktop.
Save Hounddog/39e0cb667d2dd1f2f946 to your computer and use it in GitHub Desktop.
'zf-apigility' => array(
'doctrine-connected' => array(
'API\\V1\\Rest\\HashTag\\HashTagResource' => array(
'object_manager' => 'doctrine.entitymanager.orm_default',
'hydrator' => 'Api\\V1\\HashTagHydrator',
),
),
),
'zf-rest' => array(
'API\\V1\\Rest\\HashTag\\Controller' => array(
'listener' => 'API\\V1\\Rest\\HashTag\\HashTagResource',
'route_name' => 'api.rest.hash-tag',
'route_identifier_name' => 'hash_tag_id',
'entity_identifier_name' => 'id',
'collection_name' => 'hash_tag',
'entity_http_methods' => array(
0 => 'GET',
1 => 'PATCH',
2 => 'PUT',
3 => 'DELETE',
),
'collection_http_methods' => array(
0 => 'GET',
1 => 'POST',
2 => 'PATCH',
),
'collection_query_whitelist' => array(),
'page_size' => 25,
'page_size_param' => null,
'entity_class' => 'API\\V1\\Entity\\HashTag',
'collection_class' => 'API\\V1\\Rest\\HashTag\\HashTagCollection',
),
<?php
namespace API\V1\Rest\HashTag;
use ZF\Apigility\Doctrine\Server\Resource\DoctrineResource;
class HashTagResource extends DoctrineResource
{
public function findByType($type) {
$collection = $this->getObjectManager()->getRepository('API\\V1\\Entity\\HashTag')->findBy(
array(
'type' => $type,
)
);
return $collection;
}
public function findByName($name)
{
return $this->getObjectManager()->getRepository('API\\V1\\Entity\\HashTag')->findOneBy(array(
'name' => $name
));
}
}
public function refreshAction()
{
$sm = $this->getServiceLocator();
$em = $sm->get('doctrine.entitymanager.orm_default');
$hashtagResource = $sm->get('API\\V1\\Rest\\HashTag\\HashTagResource');
$itemService = $sm->get('page_service_item');
$client = $sm->get('HD\Instagram\Client');
$itemMapper = $itemService->getMapper();
$hashtags = $hashtagResource->findByType('instagram');
foreach($hashtags as $tag) {
$results = $client->api('tags')->recent($tag->getName());
$counter = 1;
$hashtagItem = $hashtagResource->findByName($tag->getName());
$site = $hashtagItem->getSite();
foreach($results as $result)
{
if($counter++ > 19) {
break;
}
if($itemMapper->findByItemId($result->id, 'instagram')){
echo 'found duplicate' . "\r\n";
continue;
}
$objDateTime = new \DateTime(Date('c', ($result->created_time)));
$data['type'] = 'instagram';
$data['item_id'] = $result->id;
$data['object'] = $result;
$data['createdDate'] = $objDateTime;
$data['tag'] = $tag->getName();
$entity = $itemService->create($data);
$entity->setSite($site);
$em->persist($entity);
}
$em->flush();
$em->clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment