1+ <?php
2+ /**
3+ * Copyright 2018 Google Inc.
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+
18+ namespace Google \Cloud \Samples \Jobs ;
19+
20+
21+ use Google_Service_JobService_JobQuery ;
22+ use Google_Service_JobService_RequestMetadata ;
23+ use Google_Service_JobService_SearchJobsRequest ;
24+ use Google_Service_JobService_SearchJobsResponse ;
25+ use Symfony \Component \Console \Command \Command ;
26+ use Symfony \Component \Console \Input \InputInterface ;
27+ use Symfony \Component \Console \Output \OutputInterface ;
28+
29+ /**
30+ * The sample in this file introduce how to do a email alert search.
31+ */
32+ final class EmailAlertSearchSample extends Command
33+ {
34+ # [START search_for_alerts]
35+ /**
36+ * Search jobs for alert.
37+ *
38+ * @param string|null $companyName
39+ * @return Google_Service_JobService_SearchJobsResponse
40+ */
41+ public static function search_for_alerts (string $ companyName = null )
42+ {
43+ // Make sure to set the requestMetadata the same as the associated search request
44+ $ requestMetadata = new Google_Service_JobService_RequestMetadata ();
45+ // Make sure to hash your userID
46+ $ requestMetadata ->setUserId ('HashedUserId ' );
47+ // Make sure to hash the sessionID
48+ $ requestMetadata ->setSessionId ('HashedSessionId ' );
49+ // Domain of the website where the search is conducted
50+ $ requestMetadata ->setDomain ('www.google.com ' );
51+
52+ $ request = new Google_Service_JobService_SearchJobsRequest ();
53+ $ request ->setRequestMetadata ($ requestMetadata );
54+ $ request ->setMode ('JOB_SEARCH ' );
55+ if (isset ($ companyName )) {
56+ $ jobQuery = new Google_Service_JobService_JobQuery ();
57+ $ jobQuery ->setCompanyNames (array ($ companyName ));
58+ $ request ->setQuery ($ jobQuery );
59+ }
60+
61+ $ response = JobServiceConnector::get_job_service ()->jobs ->searchForAlert ($ request );
62+ var_export ($ response );
63+ return $ response ;
64+ }
65+
66+ # [END search_for_alerts]
67+
68+ protected function configure ()
69+ {
70+ $ this
71+ ->setName ('email-alert-search ' )
72+ ->setDescription ('Run email alert search sample script. ' );
73+ }
74+
75+ protected function execute (InputInterface $ input , OutputInterface $ output )
76+ {
77+ $ companyToBeCreated = BasicCompanySample::generate_company ();
78+ $ companyName = BasicCompanySample::create_company ($ companyToBeCreated )->getName ();
79+
80+ $ jobToBeCreated = BasicJobSample::generate_job_with_required_fields ($ companyName );
81+ $ jobName = BasicJobSample::create_job ($ jobToBeCreated )->getName ();
82+
83+ // Wait several seconds for post processing.
84+ sleep (10 );
85+ self ::search_for_alerts ($ companyName );
86+
87+ BasicJobSample::delete_job ($ jobName );
88+ BasicCompanySample::delete_company ($ companyName );
89+ }
90+ }
0 commit comments