Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor Implicit Fetch doesn't work with reportstats. #76

Closed
red-crown opened this issue Mar 12, 2015 · 1 comment
Closed

Cursor Implicit Fetch doesn't work with reportstats. #76

red-crown opened this issue Mar 12, 2015 · 1 comment
Assignees
Labels

Comments

@red-crown
Copy link

The response for the <Ad_acct>/reportstats endpoint returns the paging information as follows:

{
  "paging": {
    "next": "<the_url>",
    "previous": "<the_url>",
  }
}

However, the cursor expects to see ["paging"]["cursor"]["after"], as which is returned in the responses for the other endpoints.

Is this a new change to the API?

@pruno pruno added the bug label Mar 19, 2015
@pruno pruno self-assigned this Mar 19, 2015
@red-crown
Copy link
Author

For what it's worth, I've been using something like this as a wrapper.

<?php namespace PayPerClick\Market\Facebook\Data;

use FacebookAds\Cursor;

/**
 * Class ReportCursor
 *
 * @package PayPerClick\Market\Facebook\Data
 */
class ReportCursor implements \Iterator, \Countable, \ArrayAccess {

    /**
     * @type int
     */
    protected $position = 0;

    /**
     * @type Cursor[]
     */
    protected $cursors = [];

    /**
     * @param Cursor $cursor
     */
    public function __construct(Cursor $cursor) {
        $cursor->setUseImplicitFetch(false);
        $this->cursors[] = $cursor;
    }

    /**
     * @return Cursor
     */
    public function getCursor() {
        return $this->cursors[ $this->position ];
    }

    public function current() {
        return $this->getCursor()->current()->getData();
    }

    public function next() {
        $this->getCursor()->next();
        if ($this->getCursor()->key() === null) {
            $this->advanceCursors();
        }
    }

    protected function advanceCursors() {
        if ($this->hasCursor($this->position+1)) {
            $this->getCursor()->rewind();
            $this->position++;
        } else if ($this->hasNextPage()) {
            $this->fetchNext();
        }
    }

    /**
     * @return bool
     */
    protected function hasNextPage() {
        return $this->getNextPage() !== null;
    }

    /**
     * @return string|null
     */
    protected function getNextPage() {
        $content = $this->getCursor()->getLastResponse()->getContent();

        return isset($content['paging']['next']) ? $content['paging']['next'] : null;
    }

    /**
     * @param int $offset
     * @return bool
     */
    protected function hasCursor($offset) {
        return isset($this->cursors[ $offset ]);
    }

    protected function fetchNext() {
        parse_str(parse_url($this->getNextPage(), PHP_URL_QUERY), $previousParams);

        $objectPrototype = clone $this->getCursor()->offsetGet($this->getCursor()->getIndexRight());
        $request         = $this->getCursor()->getLastResponse()->getRequest()->createClone();

        $request->getQueryParams()->offsetSet('offset', $previousParams['offset']);

        $this->getCursor()->rewind();

        $this->position++;

        $this->cursors[ $this->position ] = new Cursor($request->execute(), $objectPrototype);
    }

    public function key() {
        return $this->getCursor()->key();
    }

    public function valid() {
        return $this->getCursor()->valid();
    }

    public function rewind() {
        $this->position = 0;
    }

    public function offsetExists($offset) {
        return $this->getCursor()->offsetExists($offset);
    }

    public function offsetGet($offset) {
        return $this->getCursor()->offsetGet($offset);
    }

    public function offsetSet($offset, $value) {
        $this->getCursor()->offsetSet($offset, $value);
    }

    public function offsetUnset($offset) {
        $this->getCursor()->offsetUnset($offset);
    }

    public function count() {
        return array_reduce($this->cursors, function ($a, $cursor) {
            return $a + $cursor->count();
        }, 0);
    }
}

@pruno pruno closed this as completed in 22a17f8 May 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants