ServerGroveLiveChat /src/ServerGrove/SGLiveChatBundle/Document/VisitRepository.php

Language PHP Lines 93
MD5 Hash 3988d5ecaa31601ae0ca74fe26c4cc69
Repository https://github.com/casoetan/ServerGroveLiveChat.git View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php

namespace ServerGrove\SGLiveChatBundle\Document;

use Doctrine\ODM\MongoDB\DocumentRepository;
use MongoDate;

/**
 * Description of VisitRepository
 *
 * @author Ismael Ambrosi<ismael@servergrove.com>
 */
class VisitRepository extends DocumentRepository
{

    /**
     * @return ServerGrove\SGLiveChatBundle\Document\Visit
     */
    public function create(Visitor $visitor)
    {
        $visit = new Visit();
        $visit->setVisitor($visitor);
        $visit->setKey(md5(time() . $visitor->getAgent() . $visitor->getId()));
        #$visit->setLocalTime($localTime);


        return $visit;
    }

    /**
     * @return ServerGrove\SGLiveChatBundle\Document\Visit
     */
    public function getByKey($key, Visitor $visitor)
    {
        $visit = null;
        if (!is_null($key)) {
            $visit = $this->findOneBy(array(
                'key' => $key));
        }

        if (!$visit) {
            $visit = $this->create($visitor);
            $this->getDocumentManager()->persist($visit);
            $this->getDocumentManager()->flush();
        }

        return $visit;
    }

    public function getLastVisits()
    {
        return $this->createQueryBuilder()->field('updatedAt')->range(new MongoDate(time() - 200), new MongoDate(time()))->field('hits')->exists(true)->getQuery()->execute();
    }

    public function getLastVisitsArray()
    {
        $array = array();
        $visits = $this->getLastVisits();
        /* @var $visit Visit */
        foreach ($visits as $visit) {
            $hits = $visit->getHits();
            $array[] = array(
                'id' => $visit->getId(),
                'visitor' => array(
                    'id' => $visit->getVisitor()->getId(),
                    'visits' => count($visit->getVisitor()->getVisits()),
                    'languages' => $visit->getVisitor()->getLanguages(),
                    'agent' => $visit->getVisitor()->getAgent(),
                    'currentPage' => $visit->getHits()->last()->getVisitLink()->getUrl(),
                    'referer' => $hits->last()->getReferer()),
                'hits' => array_map(
                function (VisitHit $hit)
                {
                    return array(
                        'id' => $hit->getId(),
                        'createdAt' => $hit->getCreatedAt()->format('Y-m-d H:i:s'),
                        'duration' => 0,
                        'link' => $hit->getVisitLink()->getUrl(),
                        'referer' => $hit->getReferer());
                }, $hits->toArray(true)),
                'localtime' => date('r', (int) $visit->getLocalTime()),
                'hostname' => /* gethostbyaddr($visit->getRemoteAddr()) */'Unknown',
                'remoteAddr' => $visit->getRemoteAddr(),
                'country' => 'unknown',
                'createdAt' => $visit->getCreatedAt()->format('Y-m-d H:i:s'),
                'lastHit' => 'lasthit',
                'duration' => time() - $visit->getCreatedAt()->format('U'));
        }

        return $array;
    }

}
Back to Top