Created
May 6, 2015 20:52
-
-
Save svolobuev/52aefa56abf0b302175c to your computer and use it in GitHub Desktop.
Vk Authorize (sf2 bundle)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\Api; | |
use PrestoHeads\VkBotBundle\VkAuthorize\VkAuthorize; | |
abstract class ApiResource | |
{ | |
/** | |
* @var VkApi | |
*/ | |
private $api; | |
/** | |
* @var VkAuthorize | |
*/ | |
protected $vkAuth; | |
/** | |
* @var array | |
*/ | |
protected $authData; | |
protected $token = null; | |
public function __construct(VkApi $api, VkAuthorize $vkAuth, $authData) | |
{ | |
$this->api = $api; | |
$this->vkAuth = $vkAuth; | |
$this->authData = $authData; | |
} | |
/** | |
* @return VkApi | |
*/ | |
protected function getApi() | |
{ | |
$this->api->setAccessToken($this->getToken()); | |
return $this->api; | |
} | |
/** | |
* @return null|string | |
* @throws \Exception | |
*/ | |
protected function getToken() | |
{ | |
if (is_null($this->token)) { | |
$this->token = $this->vkAuth->getAccessToken($this->authData['login'],$this->authData['password'],'desktop'); | |
} | |
return $this->token; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\Api\Group; | |
use PrestoHeads\VkBotBundle\Api\ApiResource; | |
class Board extends ApiResource | |
{ | |
public function deleteTopic($group_id, $topic_id) | |
{ | |
$response = $this->getApi()->api('board.deleteTopic',[ | |
'group_id' => $group_id, | |
'topic_id' => $topic_id | |
],true); | |
return $response; | |
} | |
public function restoreTopic($group_id, $topic_id) | |
{ | |
throw new \Exception("not implemented"); | |
$response = $this->getApi()->api('board.restoreTopic',[ | |
'group_id' => $group_id, | |
'topic_id' => $topic_id | |
],true); | |
return $response; | |
} | |
public function deleteComment($group_id, $topic_id, $comment_id) | |
{ | |
$response = $this->getApi()->api('board.deleteComment',[ | |
'group_id' => $group_id, | |
'topic_id' => $topic_id, | |
'comment_id' => $comment_id | |
],true); | |
return $response; | |
} | |
public function restoreComment($group_id, $topic_id, $comment_id) | |
{ | |
$response = $this->getApi()->api('board.restoreComment',[ | |
'group_id' => $group_id, | |
'topic_id' => $topic_id, | |
'comment_id' => $comment_id | |
],true); | |
return $response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\Api\Group; | |
use PrestoHeads\VkBotBundle\Api\ApiResource; | |
class Photo extends ApiResource | |
{ | |
/** | |
* Delete comment from post group photo | |
* @param $group_id | |
* @param $comment_id | |
* @return mixed | |
* @throws \Novanova\VK\VKException | |
*/ | |
public function deleteComment($group_id, $comment_id) | |
{ | |
$response = $this->getApi()->api('photo.deleteComment',[ | |
'owner_id' => "-{$group_id}", | |
'comment_id' => $comment_id | |
],true); | |
return $response; | |
} | |
/** | |
* Restore comment from post group photo | |
* @param $group_id | |
* @param $comment_id | |
* @return mixed | |
* @throws \Novanova\VK\VKException | |
*/ | |
public function restoreComment($group_id, $comment_id) | |
{ | |
$response = $this->getApi()->api('photo.restoreComment',[ | |
'owner_id' => "-{$group_id}", | |
'comment_id' => $comment_id | |
],true); | |
return $response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\Api\Group; | |
use PrestoHeads\VkBotBundle\Api\ApiResource; | |
class Video extends ApiResource | |
{ | |
/** | |
* Delete comment from post group video | |
* @param $group_id | |
* @param $comment_id | |
* @return mixed | |
* @throws \Novanova\VK\VKException | |
*/ | |
public function deleteComment($group_id, $comment_id) | |
{ | |
$response = $this->getApi()->api('video.deleteComment',[ | |
'owner_id' => "-{$group_id}", | |
'comment_id' => $comment_id | |
],true); | |
return $response; | |
} | |
/** | |
* Restore comment from post group video | |
* @param $group_id | |
* @param $comment_id | |
* @return mixed | |
* @throws \Novanova\VK\VKException | |
*/ | |
public function restoreComment($group_id, $comment_id) | |
{ | |
$response = $this->getApi()->api('video.restoreComment',[ | |
'owner_id' => "-{$group_id}", | |
'comment_id' => $comment_id | |
],true); | |
return $response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\Api\Group; | |
use PrestoHeads\VkBotBundle\Api\ApiResource; | |
class Wall extends ApiResource | |
{ | |
/** | |
* Delete post from group wall | |
* @param $group_id | |
* @param $post_id | |
* @return mixed | |
* @throws \Novanova\VK\VKException | |
*/ | |
public function delete($group_id, $post_id) | |
{ | |
$response = $this->getApi()->api('wall.delete',[ | |
'owner_id' => "-{$group_id}", | |
'post_id' => $post_id | |
],true); | |
return $response; | |
} | |
/** | |
* Restore post from group wall | |
* @param $group_id | |
* @param $post_id | |
* @return mixed | |
* @throws \Novanova\VK\VKException | |
*/ | |
public function restore($group_id, $post_id) | |
{ | |
$response = $this->getApi()->api('wall.restore',[ | |
'owner_id' => "-{$group_id}", | |
'post_id' => $post_id | |
],true); | |
return $response; | |
} | |
/** | |
* Delete comment from post group wall | |
* @param $group_id | |
* @param $comment_id | |
* @return mixed | |
* @throws \Novanova\VK\VKException | |
*/ | |
public function deleteComment($group_id, $comment_id) | |
{ | |
$response = $this->getApi()->api('wall.deleteComment',[ | |
'owner_id' => "-{$group_id}", | |
'comment_id' => $comment_id | |
],true); | |
return $response; | |
} | |
/** | |
* Restore comment from post group wall | |
* @param $group_id | |
* @param $comment_id | |
* @return mixed | |
* @throws \Novanova\VK\VKException | |
*/ | |
public function restoreComment($group_id, $comment_id) | |
{ | |
$response = $this->getApi()->api('wall.restoreComment',[ | |
'owner_id' => "-{$group_id}", | |
'comment_id' => $comment_id | |
],true); | |
return $response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\Api; | |
use Novanova\VK\VK; | |
class VkApi extends VK | |
{ | |
/** | |
* @param string $app_id | |
* @param string $secret | |
* @param string $version | |
* @param string $lang | |
* @param int $https | |
*/ | |
public function __construct($app_id, $secret, $version = '5.24', $lang = 'ru', $https = 1) | |
{ | |
parent::__construct($app_id, $secret, $version, $lang, $https); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Component\DomCrawler\Crawler; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
class DefaultController extends Controller | |
{ | |
/** | |
* @Route("/vk/bot/get_token") | |
*/ | |
public function getTokenAction() | |
{ | |
$time = microtime(true); | |
/** @var \PrestoHeads\VkBotBundle\VkAuthorize\VkAuthorize $auth */ | |
$auth = $this->get('vk_bot.authorize'); | |
$bot = $this->container->getParameter('vk_bot.bot'); | |
$token = $auth->getAccessToken($bot['login'],$bot['password'],'desktop'); | |
$time = microtime(true) - $time; | |
return new JsonResponse([ | |
'success'=>true, | |
'token' => $token, | |
'time_execute'=> $time | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\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('presto_heads_vk_bot'); | |
// Here you should define the parameters that are allowed to | |
// configure your bundle. See the documentation linked above for | |
// more information on that topic. | |
return $treeBuilder; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\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 PrestoHeadsVkBotExtension 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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle; | |
use PrestoHeads\VkBotBundle\DependencyInjection\CompilerPass\VkAuthorizeCompilerPass; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\HttpKernel\Bundle\Bundle; | |
class PrestoHeadsVkBotBundle extends Bundle | |
{ | |
public function build(ContainerBuilder $container) | |
{ | |
parent::build($container); | |
$container->addCompilerPass(new VkAuthorizeCompilerPass()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
vk_bot.authorize.class: PrestoHeads\VkBotBundle\VkAuthorize\VkAuthorize | |
vk_bot.authorize.desktop.class: PrestoHeads\VkBotBundle\VkAuthorize\Adapter\DesktopApplicationAuthorize | |
vk_bot.api.class: PrestoHeads\VkBotBundle\Api\VkApi | |
vk_bot.app.id: ~ | |
vk_bot.app.secret: ~ | |
vk_bot.authorize.desktop.permissions: 6274559 | |
vk_bot.authorize.desktop.offline_token: true | |
vk_bot.bot: | |
login: ~ | |
password: ~ | |
services: | |
vk_bot.api: | |
class: %vk_bot.api.class% | |
arguments: [%vk_bot.app.id%, %vk_bot.app.secret%, '5.24', 'ru', 1] | |
vk_bot.authorize: | |
class: %vk_bot.authorize.class% | |
vk_bot.authorize.desktop: | |
class: %vk_bot.authorize.desktop.class% | |
arguments: [%vk_bot.app.id%, %vk_bot.app.secret%, %vk_bot.authorize.desktop.permissions%, %vk_bot.authorize.desktop.offline_token%] | |
tags: | |
- { name: vk_bot.authorize.adapter } | |
- { name: vk_bot.authorize.availableAuthType } | |
vk_bot.api.group.wall: | |
class: PrestoHeads\VkBotBundle\Api\Group\Wall | |
arguments: [@vk_bot.api, @vk_bot.authorize, %vk_bot.bot%] | |
vk_bot.api.group.board: | |
class: PrestoHeads\VkBotBundle\Api\Group\Board | |
arguments: [@vk_bot.api, @vk_bot.authorize, %vk_bot.bot%] | |
vk_bot.api.group.photo: | |
class: PrestoHeads\VkBotBundle\Api\Group\Photo | |
arguments: [@vk_bot.api, @vk_bot.authorize, %vk_bot.bot%] | |
vk_bot.api.group.video: | |
class: PrestoHeads\VkBotBundle\Api\Group\Video | |
arguments: [@vk_bot.api, @vk_bot.authorize, %vk_bot.bot%] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> | |
<file source-language="en" datatype="plaintext" original="file.ext"> | |
<body> | |
<trans-unit id="1"> | |
<source>Symfony2 is great</source> | |
<target>J'aime Symfony2</target> | |
</trans-unit> | |
</body> | |
</file> | |
</xliff> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello bot {{ name }}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PrestoHeads\VkBotBundle\Tests\Controller; | |
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
class DefaultControllerTest extends WebTestCase | |
{ | |
public function testIndex() | |
{ | |
$client = static::createClient(); | |
$crawler = $client->request('GET', '/hello/Fabien'); | |
$this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment